-1
Array
(
    [0] => {'error': None, 'result': {'asks': [['7.5197', '13231.93']], 'bids': [['7.5101', '0.9']]}}
)

I am getting an array answer like this from a web site with an api query. In this array I want to get 7.5197 and 7.5101. How can I get these two number in php? If you can help me I would be appreciated.

brombeer
  • 8,716
  • 5
  • 21
  • 27
canbati
  • 1
  • 1

1 Answers1

0

Your Response

   $str = "{'error': None, 'result': {'asks': [['7.5197', '13231.93']], 'bids': [['7.5101', '0.9']]}}";";

look like a json string, but isn't. With some substitutions it can be transformed into a valid json string.

$jsonStr = strtr($str, ["'" => '"', 'None' => '"None"']);

If there is already an array or an object in PHP, this steps can be omitted.

Now an object/array can be created with json_decode and the values can be accessed.

$obj = json_decode($jsonStr);

$valAsks = $obj->result->asks[0][0];  //"7.5197"
$valBids = $obj->result->bids[0][0];  //"7.5101"
jspit
  • 7,276
  • 1
  • 9
  • 17