PHP 读取Postgresql中的数组

 1 function getarray_postgresql($arraystr)
 2 {
 3     $regx1 = '/^{(.*)}$/';
 4     $regx2 = "/\"((\\\\\\\\|\\\\\"|[^\"])+)\"|[^,]+/";
 5     $regx3 = '/^[^"].*$|^"(.*)"$/';
 6     $match = null;
 7     preg_match( $regx1,$arraystr,$match);
 8     $str = $match[1];
 9     preg_match_all($regx2, $str,$match);
10     $items = $match[0];
11     $array = array();
12     $count = count($items);
13     for($index = 0; $index < $count;++$index)
14     {
15         preg_match($regx3, $items[$index],$match);
16         $array[$index]=end($match);
17     }
18     return $array;
19 }

在PHP从postgresql中读取的数据都是字符串的,一般的数据还好处理,但是postgresql有一种数组型的数据,而如果我们的数组是字符串的,前且,里面有逗号或斜线也是可能的,这就给我们读取带来了一定的麻烦,上面的函数是我奋斗了几个小时写出来的。尽可能的考虑到了斜线,逗号,引号的存在。