1

in php i get an array from pgsql:

$res = $db
    ->query( "SELECT myarray FROM arrays WHERE array_name=smth" )
    ->fetchRow();
exit (json_encode($res));

Type in pgsql is integer[]

But in JS I get JSON with one string "{1,2,3,...,}" How to get in 'array-way' this data?

4 Answers 4

2

PDO does not do any kind of type translation, everything is returned as strings. So you will have to parse the string using explode() and build a PHP array yourself.

Sign up to request clarification or add additional context in comments.

Comments

1

json_encode Returns a JSON encoded string on success.

Assign the values returned by fetchrow() to a variable and try to print it before json_encode . it will be an array!!!

Comments

1

If you want to get the JSON decoded to an array in JavaScript, you can simply use the function eval(). But first you should make sure it is a JSON string. See more: http://json.org/js.html

2 Comments

problem is that php get not array() from pgsql but string. I want to get php array and that json_encode will do everything for me without eval
Why don't you serialize the variable before insert it into the database, and then you just have to reverse it. Check serialize() and unserialize(). If you don't want to do that, you'll need to parse them manually.
0

You can use something like this:

$php_array = json_decode( str_replace( 
    array( '[',      ']',      '{', '}' ), 
    array( '\u005B', '\u005D', '[', ']' ), 
    $your_pgsql_array 
) );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.