-5

I've read every single question on StackOverflow on how to parse JSON with PHP but none of them are working with the JSON I have.

array(2) { ["status"]=> bool(true) ["currency"]=> array(30) { [0]=> array(10) { ["currency"]=> string(7) "EUR/AUD" ["value"]=> string(6) "1.4925" ["change"]=> string(7) "-0.0076" ["change_percent"]=> string(8) "-0.50663" ["ask"]=> string(6) "1.4926" ["bid"]=> string(6) "1.4923" ["daily_lowest"]=> string(6) "1.4873" ["daily_highest"]=> string(6) "1.5016" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=> string(8) "original" } [1]=> array(10) { ["currency"]=> string(7) "EUR/CAD" ["value"]=> string(5) "1.494" ["change"]=> string(7) "-0.0032" ["change_percent"]=> string(8) "-0.21373" ["ask"]=> string(5) "1.494" ["bid"]=> string(6) "1.4939" ["daily_lowest"]=> string(5) "1.489" ["daily_highest"]=> string(6) "1.4986" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=> string(8) "original" } [2]=> array(10) { ["currency"]=> string(7) "EUR/CHF" ["value"]=> string(6) "1.1434" ["change"]=> string(6) "0.0024" ["change_percent"]=> string(7) "0.21034" ["ask"]=> string(6) "1.1435" ["bid"]=> string(6) "1.1434" ["daily_lowest"]=> string(6) "1.1402" ["daily_highest"]=> string(6) "1.1444" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=> string(8) "original" } [3]=> array(10) { ["currency"]=> string(7) "EUR/GBP" ["value"]=> string(6) "0.9099" ["change"]=> string(7) "-0.0019" ["change_percent"]=> string(8) "-0.20838" ["ask"]=> string(4) "0.91" ["bid"]=> string(6) "0.9099" ["daily_lowest"]=> string(6) "0.9083" ["daily_highest"]=> string(6) "0.9144" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=> string(8) "original" } [4]=> array(10) { ["currency"]=> string(7) "EUR/ILS" ["type"]=> string(9) "converted" ["date"]=> string(19) "2017-08-16 09:46:11" ["value"]=> string(7) "4.24323" ["change"]=> string(3) "0.0" ["change_percent"]=> string(3) "0.0" ["ask"]=> string(3) "0.0" ["bid"]=> string(3) "0.0" ["daily_lowest"]=> string(3) "0.0" ["daily_highest"]=> string(3) "0.0" } [5]=> array(10) { ["currency"]=> string(7) "EUR/JPY" ["value"]=> string(8) "130.1145" ["change"]=> string(6) "0.2425" ["change_percent"]=> string(7) "0.18672" ["ask"]=> string(7) "130.122" ["bid"]=> string(7) "130.107" ["daily_lowest"]=> string(7) "129.613" ["daily_highest"]=> string(7) "130.405" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=> string(8) "original" } [6]=> array(10) { ["currency"]=> string(7) "EUR/NZD" ["value"]=> string(6) "1.6182" ["change"]=> string(7) "-0.0036" ["change_percent"]=> string(8) "-0.22198" ["ask"]=> string(6) "1.6186" ["bid"]=> string(6) "1.6178" ["daily_lowest"]=> string(6) "1.6138" ["daily_highest"]=> string(6) "1.6247" ["date"]=> string(19) "2017-08-16 09:46:11" ["type"]=> string(8) "original" } [7]=> array(10).............

I'm trying to output the value of each ["currency"] ["bid"] with this code:

foreach($result['currency'][0]['bid'] as $item) {
    print $item['currency'][0]['bid'];
}

And tried 100 other variations, still getting errors like:

Warning: Illegal string offset 'currency' in getData.php on line 27
Warning: Illegal string offset 'value' in getData.php on line 27
Warning: Invalid argument supplied for foreach() in getData.php on line 27

Maybe there are online parser websites that can help with that? How do I solve this?

Ricardo
  • 1,653
  • 8
  • 26
  • 51
  • 6
    That is not valid JSON, it rather looks like the output of `var_dump()`. – Björn Tantau Aug 16 '17 at 08:55
  • Yes it's probably a var dump of a json decode to array. – Andreas Aug 16 '17 at 08:59
  • You don't have problems with `JSON`s, it seems you need to read more about how to use [PHP arrays](http://php.net/manual/en/language.types.array.php#language.types.array.syntax.accessing). And – axiac Aug 16 '17 at 08:59
  • Wasn't it value you asked for just a few minutes ago? Have you edited the question? – Andreas Aug 16 '17 at 09:02

1 Answers1

1

It seems to me like it should be.

foreach($result['currency']as $item) {
    print $item['value'];
 }

Because each currency is 0,1,2 and so on.
And in the item 0,1,2 there is the "value".

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • Thanks! I've also noticed that I was using `var_dump` like others suggested in the comments. It's working now with a few exceptions where I see `Notice: Undefined index: bid in` a few times between the output. EDIT: There is just no `bid` value in those lines. Thanks for help out! – Ricardo Aug 16 '17 at 09:05