Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I couldn't convert postgresql array to php array. Here is my array column is character varying[] and here are the values {image1,image2,image3}

$imgArry always null.

$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];
share|improve this question

1 Answer 1

This does it.

 preg_match('/^{(.*)}$/', $row["images"], $matches);
 $imgArry=str_getcsv($matches[1]);
share|improve this answer
    
This is not immuned to slashed expressions. str_getcsv(str_replace('\\\\', '\\', trim($data, "{}")))) is. –  greg Jun 25 '13 at 12:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.