2013-06-24 50 views
1

我无法将postgresql数组转换为php数组。 这里是我的阵列 列字符改变[]和这里的值 {图像1,图像2,图像3}查询postgresql数组列

$ imgArry总是空。

$query = "SELECT \"A\".\"id\", 
       array_to_json(\"A\".images) 
       FROM 
       public.A " ; 

    $rs = pg_query($db, $query) or 
      die("Cannot execute query: $query\n"); 
    $index = 0; 
    while ($row = pg_fetch_assoc($rs)) { 
     $A->id = $row["id"]; 

     $imgArry = json_decode($row["images"]); 
     $planproduct->image=$imgArry[0]; 

回答

1

这就行了。

preg_match('/^{(.*)}$/', $row["images"], $matches); 
$imgArry=str_getcsv($matches[1]); 
+0

这不是免于削减的表达式。 'str_getcsv(str_replace('\\\\','\\',trim($ data,“{}”))))'is。 – greg