0

I try to read IME field in PHP from following JSON FORMAT:

{"komentarji": [ {"RC_KOMENTARJI": [ {
          "K_ID": 101,
          "STATUS": "A",
          "IME": "boris",
          "E_MAIL": "test@example.com",
          "KOMENTAR": "testni vnos",
          "IP": "10.0.0.6",
          "DATUM_ZAPISA": "2016-12-03T23:23:47Z",
          "DATUM_UREJANJA": "2016-12-03T23:24:01Z"
        },
        {
          "K_ID": 1,
          "STATUS": "A",
          "IME": "Peter",
          "KOMENTAR": "Zelo profesionalno ste opravili svoje delo.",
          "IP": "10.0.0.8",
          "DATUM_ZAPISA": "2011-05-04T00:00:00Z"
        }
      ] } ] }

How can I reach that field via foreach in PHP? Thank you.

Cooler
  • 45
  • 1
  • 8
  • 5
    Possible duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Rajdeep Paul Dec 04 '16 at 10:04

3 Answers3

1

Let you decode the json to object named $result.

If you want to read first IME then try this

$result->komentarji[0]->RC_KOMENTARJI[0]->IME

If you want to read all IME then you have to apply loop throw komentarji and RC_KOMENTARJI

Md. Sahadat Hossain
  • 3,210
  • 4
  • 32
  • 55
0

Decode it by using json_decode().

$object = json_decode($json);
// result in object
$array =  json_decode($json, true);
// result in array 
Dave
  • 3,073
  • 7
  • 20
  • 33
0

You can try this:

$array = json_decode($json, true);

foreach ($array['komentarji'] as $key => $value) {
    foreach ($value['RC_KOMENTARJI'] as $k => $val) {
        echo $val['IME'] . "<br/>";
    }
}

This will print:

boris
Peter

Hope it helps!!!

Kostas Drak
  • 3,222
  • 6
  • 28
  • 60