Get usable array from a curl response, which is formatted as a php array Get usable array from a curl response, which is formatted as a php array curl curl

Get usable array from a curl response, which is formatted as a php array


The data you are getting is probably not an array, but a string containing an array structure, e.g. output by print_r(). This kind of data will not automatically be converted back into a PHP array.

If you can control the page you are querying this from, encode the data using a method like serialize() or json_encode() and on the querying side, decode the data you get from curl using (unserialize() or json_decode()) respectively. Those functions will give you a proper PHP array.

If you have no way to change the way the URL outputs its data, the only way I can see is (yuck!) using eval() - I can elaborate on that if need be, but it's a really really bad idea.


Your $outputArray is a string, that seems to contain something like the ouput of print_r().

There is no way PHP can guess that string represents an array -- and it's not really close to the syntax that's used to declare an array ; so this will not work.


A solution would be :

  • to modify the remote script you're calling, so it returns a string containing some serialized data
    • i.e. and array, serialized with serialize
    • or with json_encode
  • And, on your side, unserialize the data, to get the array back,
    • with either unserialize
    • or json_decode