将 PostgreSQL 数组转换为 PHP 数组 [英] Convert PostgreSQL array to PHP array
本文介绍了将 PostgreSQL 数组转换为 PHP 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 PHP 中读取 Postgresql 数组时遇到问题.我尝试过explode(),但这会破坏字符串中包含逗号的数组,并且str_getcsv()
但这也不好,因为PostgreSQL 没有引用日语字符串.
I have trouble reading Postgresql arrays in PHP. I have tried explode(), but this breaks arrays containing commas in strings, and str_getcsv()
but it's also no good as PostgreSQL doesn't quote the Japanese strings.
不工作:
explode(',', trim($pgArray['key'], '{}'));
str_getcsv( trim($pgArray['key'], '{}') );
示例:
// print_r() on PostgreSQL returned data: Array ( [strings] => {または, "some string without a comma", "a string, with a comma"} )
// Output: Array ( [0] => または [1] => "some string without a comma" [2] => "a string [3] => with a comma" )
explode(',', trim($pgArray['strings'], '{}'));
// Output: Array ( [0] => [1] => some string without a comma [2] => a string, with a comma )
print_r(str_getcsv( trim($pgArray['strings'], '{}') ));
推荐答案
如果你有 PostgreSQL 9.2 你可以这样做:
If you have PostgreSQL 9.2 you can do something like this:
SELECT array_to_json(pg_array_result) AS new_name FROM tbl1;
结果将以 JSON 形式返回数组
The result will return the array as JSON
然后是php方面的问题:
Then on the php side issue:
$array = json_decode($returned_field);
您也可以转换回来.这是JSON 函数页面
You can also convert back. Here are the JSON functions page
这篇关于将 PostgreSQL 数组转换为 PHP 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文