PHP 数组到 postgres 数组
现在 php 无法直接使用 Postgresql 数组。例如,php 采用 postgresql 数组,例如 '{"foo","bar"}'
我需要简单的 php 函数从 php 数组创建多维 postgresql 数组。
我认为实验性 pg_convert() 不是最佳的,因为它需要额外的数据来形成数据库输出的简单数组字符串,也许我误解了这个函数的想法。
例如,我需要转换
$from=array( array( "par_1_1","par_1_2" ), array( "array_2_1", "array_2_2" ) );
$to='{{"par_1_1","par_1_2"},{"par_2_1","par_2_2"}}';
可以使用 array_walk_recursive() 来转换数组的最深元素吗?
Now php can't work directly wit Postgresql array. For example, php taking postgresql array like
'{"foo","bar"}'
I need simple php function to create multidimensional postgresql array from php array.
I think that experimental pg_convert() isn't optimal because it needs of extra data to form simple array string for database output, maybe I misunderstood the idea of this function.
For example, I need to convert
$from=array( array( "par_1_1","par_1_2" ), array( "array_2_1", "array_2_2" ) );
$to='{{"par_1_1","par_1_2"},{"par_2_1","par_2_2"}}';
Can I use array_walk_recursive() to convert the deepest elements of array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个将 PHP 数组转换为 PG 数组的简单函数。
Here's a simple function for converting a PHP array to PG array.
使用 pg_escape_string 进行引用并支持 PHP NULL 和布尔值的小编辑:
A little edit that uses pg_escape_string for quoting and that support PHP NULLS and booleans:
这与 mstefano80 的答案相同,但更易于人类阅读、通用和现代(至少对我来说):
测试:
This is the same as mstefano80's answer, but more human-readable, universal and modern (at least for me):
Tests: