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: 
As @Kelt mentioned:

> 
> Postgresql arrays look like this: {1,2,3,4}
> 
> 
> You can just simply replace first { and last } with [ and ]
> respectively and then json\_decode that.
> 
> 
> 

But his solution works only for one-dimensional arrays.

Here the solution either for one-dimensional and multidimensional arrays:

```
$postgresArray = '{{1,2},{3,4}}';
$phpArray = json_decode(str_replace(['{', '}'], ['[', ']'], $postgresArray));  // [[1,2],[3,4]]

```

To cast back:

```
$phpArray=[[1,2],[3,4]];
$postgresArray=str_replace(['[', ']'], ['{', '}'], json_encode($phpArray));

```
If you have control of the query that's hitting the database, why don't you just use [`unnest()`](https://www.postgresql.org/docs/current/static/functions-array.html) to get the results as rows instead of Postgres-arrays? From there, you can natively get a PHP-array.

```php
$result = pg_query('SELECT unnest(myArrayColumn) FROM someTable;');
if ( $result === false ) {
    throw new Exception("Something went wrong.");
}
$array = pg_fetch_all($result);

```

This sidesteps the overhead and maintenance-issues you'd incur by trying to convert the array's string-representation yourself.