BigJobbies BigJobbies - 1 month ago 4x
PHP Question

PHP - Creating an array from a string given from an API

I have a string containing that is being returned from an API which is dynamic, an example of the string is below:


I cant really do anything with it like this so i would like to split it into 2 arrays, one for the first number and one for the second, like so:


  • 1473261033000

  • 1473312464000

  • 1473313206000

  • 1473313505000

  • 1473313805000

  • 1473314105000


  • 3.7933

  • 2.0295

  • 2.0844

  • 1.4888

  • 1.3003

  • 1.1164

Would someone help me out on how i could go about this? its driving me nuts.


You need to first decode the JSON string into an array, then use array_column to extract the two arrays.

This will require PHP >=5.5, but if you require a lower version then you should be able to find a backwards compatible version of the function on the documentation page.

$str = '[[[1473261033000,3.7933],[1473312464000,2.0295],[1473313206000,2.0844],[1473313505000,1.4888],[1473313805000,1.3003],[1473314105000,1.1164]]]';
$json = json_decode($str, true)[0];

$first = array_column($json, 0);
$second = array_column($json, 1);


See for a demo.