Ear*_*ind 32 php arrays postgresql
我在PHP中阅读Postgresql数组时遇到问题.我尝试过explode(),但这打破了包含字符串逗号的数组和str_getcsv(),但它也没有用,因为PostgreSQL没有引用日语字符串.
不工作:
explode(',', trim($pgArray['key'], '{}'));
str_getcsv( trim($pgArray['key'], '{}') );
Run Code Online (Sandbox Code Playgroud)
例:
// 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'], '{}') ));
Run Code Online (Sandbox Code Playgroud)
mic*_*lbn 79
如果您有PostgreSQL 9.2,您可以这样做:
SELECT array_to_json(pg_array_result) AS new_name FROM tbl1;
Run Code Online (Sandbox Code Playgroud)
结果将数组作为JSON返回
然后在php方面问题:
$array = json_decode($returned_field);
Run Code Online (Sandbox Code Playgroud)
你也可以转换回来.这是JSON函数页面
dmi*_*kam 11
由于这些解决方案都不适用于多维数组,所以我在这里提供了一个适用于任何复杂数组的递归解决方案:
function pg_array_parse($s, $start = 0, &$end = null)
{
if (empty($s) || $s[0] != '{') return null;
$return = array();
$string = false;
$quote='';
$len = strlen($s);
$v = '';
for ($i = $start + 1; $i < $len; $i++) {
$ch = $s[$i];
if (!$string && $ch == '}') {
if ($v !== '' || !empty($return)) {
$return[] = $v;
}
$end = $i;
break;
} elseif (!$string && $ch == '{') {
$v = pg_array_parse($s, $i, $i);
} elseif (!$string && $ch == ','){
$return[] = $v;
$v = '';
} elseif (!$string && ($ch == '"' || $ch == "'")) {
$string = true;
$quote = $ch;
} elseif ($string && $ch == $quote && $s[$i - 1] == "\\") {
$v = substr($v, 0, -1) . $ch;
} elseif ($string && $ch == $quote && $s[$i - 1] != "\\") {
$string = false;
} else {
$v .= $ch;
}
}
return $return;
}
Run Code Online (Sandbox Code Playgroud)
我没有测试太多,但看起来它的工作原理.在这里你有我的测试结果:
var_export(pg_array_parse('{1,2,3,4,5}'));echo "\n";
/*
array (
0 => '1',
1 => '2',
2 => '3',
3 => '4',
4 => '5',
)
*/
var_export(pg_array_parse('{{1,2},{3,4},{5}}'));echo "\n";
/*
array (
0 =>
array (
0 => '1',
1 => '2',
),
1 =>
array (
0 => '3',
1 => '4',
),
2 =>
array (
0 => '5',
),
)
*/
var_export(pg_array_parse('{dfasdf,"qw,,e{q\"we",\'qrer\'}'));echo "\n";
/*
array (
0 => 'dfasdf',
1 => 'qw,,e{q"we',
2 => 'qrer',
)
*/
var_export(pg_array_parse('{,}'));echo "\n";
/*
array (
0 => '',
1 => '',
)
*/
var_export(pg_array_parse('{}'));echo "\n";
/*
array (
)
*/
var_export(pg_array_parse(null));echo "\n";
// NULL
var_export(pg_array_parse(''));echo "\n";
// NULL
Run Code Online (Sandbox Code Playgroud)
PS:我知道这是一个非常古老的帖子,但我找不到postgresql pre 9.2的任何解决方案
使用正则表达式将 PostgreSQL(一维)数组文字解析为 PHP 数组的可靠函数:
function pg_array_parse($literal)
{
if ($literal == '') return;
preg_match_all('/(?<=^\{|,)(([^,"{]*)|\s*"((?:[^"\\\\]|\\\\(?:.|[0-9]+|x[0-9a-f]+))*)"\s*)(,|(?<!^\{)(?=\}$))/i', $literal, $matches, PREG_SET_ORDER);
$values = [];
foreach ($matches as $match) {
$values[] = $match[3] != '' ? stripcslashes($match[3]) : (strtolower($match[2]) == 'null' ? null : $match[2]);
}
return $values;
}
print_r(pg_array_parse('{blah,blah blah,123,,"blah \\"\\\\ ,{\100\x40\t\da?\?",NULL}'));
// Array
// (
// [0] => blah
// [1] => blah blah
// [2] => 123
// [3] =>
// [4] => blah "\ ,{@@ da??
// [5] =>
// )
var_dump(pg_array_parse('{,}'));
// array(2) {
// [0] =>
// string(0) ""
// [1] =>
// string(0) ""
// }
print_r(pg_array_parse('{}'));
var_dump(pg_array_parse(null));
var_dump(pg_array_parse(''));
// Array
// (
// )
// NULL
// NULL
print_r(pg_array_parse('{???, "some string without a comma", "a string, with a comma"}'));
// Array
// (
// [0] => ???
// [1] => some string without a comma
// [2] => a string, with a comma
// )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30016 次 |
| 最近记录: |