1

I want to iterate the keys of JSON:

{
 "rates":
 {
  "AED":4.089781,
  "AFN":85.578915,
  "ALL":121.871136
 }
}

like:

key[0] = AED
key[1] = AFN .... and so on..

Or in a foreach loop

How do I do that?

Gilbert Gabriel
  • 422
  • 2
  • 8
  • 23
Computer User
  • 2,839
  • 4
  • 47
  • 69

1 Answers1

1

Use json_decode() in combination with forEach()

$j = json_decode('{
    "rates": {
        "AED":4.089781,
        "AFN":85.578915,
        "ALL":121.871136
    }}
');

foreach($j->rates as $key => $tmp) {
  echo $key . PHP_EOL;
}
# AED AFN ALL
0stone0
  • 34,288
  • 4
  • 39
  • 64