开发者

How to process array returned from pgsql?

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 ge开发者_如何学Pythont in 'array-way' this data?


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.


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!!!


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


You can use something like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜