As neither of these solutions work with multidimentional arrays, so I offer here my recursive solution that works with arrays of any complexity:
<?php
function pg_array_parse($s,$start=0,&$end=NULL){
if (empty($s) || $s[0]!='{') return NULL;
$return = array();
$br = 0;
$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;
}else
if (!$string && $ch=='{'){
$v = pg_array_parse($s,$i,$i);
}else
if (!$string && $ch==','){
$return[] = $v;
$v = '';
}else
if (!$string && ($ch=='"' || $ch=="'")){
$string = TRUE;
$quote = $ch;
}else
if ($string && $ch==$quote && $s[$i-1]=="\\"){
$v = substr($v,0,-1).$ch;
}else
if ($string && $ch==$quote && $s[$i-1]!="\\"){
$string = FALSE;
}else{
$v .= $ch;
}
}
return $return;
}
?>
I haven't tested it too much, but looks like it works.
Here you have my tests with results:
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
P.S.: I know this is a very old post, but I couldn't find any solution for postgresql pre 9.2