Question: I have trouble reading Postgresql arrays in PHP. I have tried explode(), but this breaks arrays containing commas in strings, and `str_getcsv()` but it's also no good as PostgreSQL doesn't quote the Japanese strings.

Not working:

```
explode(',', trim($pgArray['key'], '{}'));
str_getcsv( trim($pgArray['key'], '{}') );

```

Example:

```
// print_r() on PostgreSQL returned data: Array ( [strings] => {または, "some string without a comma", "a string, with a comma"} )

// Output: Array ( [0] => または [1] => "some string without a comma" [2] => "a string [3] => with a comma" ) 
explode(',', trim($pgArray['strings'], '{}'));

// Output: Array ( [0] => [1] => some string without a comma [2] => a string, with a comma ) 
print_r(str_getcsv( trim($pgArray['strings'], '{}') ));

```

Answer: 
If you have PostgreSQL 9.2 you can do something like this:

```
SELECT array_to_json(pg_array_result) AS new_name FROM tbl1;

```

The result will return the array as JSON

Then on the php side issue:

```
$array = json_decode($returned_field);

```

You can also convert back. Here are the [JSON functions](http://www.postgresql.org/docs/devel/static/functions-json.html) page
I tried the array\_to\_json answer, but unfortunalety this results in an unknown function error.
Using the dbal query builder on a postgres 9.2 database with something like `->addSelect('array_agg(a.name) as account_name')`, I got as result a string like `{ "name 1", "name 2", "name 3" }`

There are only quotes around the array parts if they contain special characters like whitespace or punctuation.

So if there are quotes, I make the string a valid json string and then use the build-in parse json function. Otherwise I use explode.

```
$data = str_replace(array("\r\n", "\r", "\n"), "", trim($postgresArray,'{}'));
if (strpos($data, '"') === 0) {
    $data = '[' . $data . ']';
    $result = json_decode($data);
} else {
    $result = explode(',', $data);

```

}