1

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];

1 Answer 1

1

This does it.

 preg_match('/^{(.*)}$/', $row["images"], $matches);
 $imgArry=str_getcsv($matches[1]);
Sign up to request clarification or add additional context in comments.

1 Comment

This is not immuned to slashed expressions. str_getcsv(str_replace('\\\\', '\\', trim($data, "{}")))) is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.