0

I have a JSON Feed which is accessed by an api. The json feed it returns is as below:

[  
   {  
      "isoDate":"2017-09-15T00:00:00.0000000",
      "events":[  
         {  
            "id":"-7317",
            "name":"Exhibition SKMU: The collection 2015-2017",
         },
         {  
            "id":"-91417",
            "name":"Torget - a multi cultural meeting place in Geilo",
         }
      ]
   },
   {  
      "isoDate":"2017-09-16T00:00:00.0000000",
      "events":[  
         {  
            "id":"-7317",
            "name":"Exhibition SKMU: The collection 2015-2017",
         },
         {  
            "id":"-91417",
            "name":"Torget - a multi cultural meeting place in Geilo",
         }
      ]
   }
]

I need the isoDate to be listed with each event instead of individually. e.g.

[  
   {  
      "events":[  
         {  
            "isoDate":"2017-09-15T00:00:00.0000000",
            "id":"-7317",
            "name":"Exhibition SKMU: The collection 2015-2017",
         },
         {  
            "isoDate":"2017-09-15T00:00:00.0000000",
            "id":"-91417",
            "name":"Torget - a multi cultural meeting place in Geilo",
         }
      ]
   },
   {  
      "events":[  
         {  
            "isoDate":"2017-09-16T00:00:00.0000000",
            "id":"-7317",
            "name":"Exhibition SKMU: The collection 2015-2017",
         },
         {  
            "isoDate":"2017-09-16T00:00:00.0000000",
            "id":"-91417",
            "name":"Torget - a multi cultural meeting place in Geilo",
         }
      ]
   }
]

Can this be achieved with php? Basically fetch that feed from a url and then display it in my preferred format?

mmc501
  • 161
  • 2
  • 10
  • 1
    yes can, you must decode that json first to get the array, after that you can try to loop that array. – Ramadhan Sep 15 '17 at 16:08

3 Answers3

4

So this is what you have to do, to get back your desired format of the json, $json is your json string:

$eventList = json_decode($json);

foreach($eventList as $eventEntry){
    $isoDate = $eventEntry->isoDate;

    foreach($eventEntry->events as $subEventEntry){
        $subEventEntry->isoDate = $isoDate;
    }

    //delete the isoDate from outer
    unset($eventEntry->isoDate);
}

echo json_encode($eventList);

So basically, you are first decoding your json into php structure, apply your changes and after that, encode it back. Note here, that I have not appened true as second parameter for the $json_decode, but working with the resulting object.

Also: Your json is not standard comform and could result in errors. PHP will properly not decode it, because your object end with a comma. The last element of an object should be without comma. Instead of

{  
    "id":"-91417",
    "name":"Torget - a multi cultural meeting place in Geilo",
}

make it like this:

{  
    "id":"-91417",
    "name":"Torget - a multi cultural meeting place in Geilo"
}

I know, this can be a problem, when you get it from an API, but this is another problem of itself...

EDIT:

To get every "events" into one big array, you have to store them just like your imagination ;) . Think it like this: $subEventEntry holds one "events"-object. Because you are iterating both levels, you see everyone object of them. My suggestion would be to store them in a new array, and recreating the structure around it:

$everything = new stdClass();
$everything->events = array();

and then, in the inner loop:

foreach($eventList as $eventEntry){
    $isoDate = $eventEntry->isoDate;

    foreach($eventEntry->events as $subEventEntry){
        $subEventEntry->isoDate = $isoDate;
        $everything->events[] = $subEventEntry; // <-- this has to be added
    }

    //delete the isoDate from outer
    unset($eventEntry->isoDate);
}

When recreating the structure, and you don't need the old structure anymore you could remove the unset.

Just remeber every [ ] pair in the json represents an array, every { } pair an object (stdClass). The name of this object/array is referenced -> by its class property in the superobject.

Fabian N.
  • 1,221
  • 10
  • 18
  • Thanks - that's working well - that json format was ok - i fotgot to remove those comma's when pasting here as there was alot of extra unecessary lines. Is it possible to combine all those events under the 1 events node: "events":[ – mmc501 Sep 18 '17 at 10:30
  • Yes, see my Edit. – Fabian N. Sep 19 '17 at 15:37
  • Thanks - had been trying various things the gist of what you were doing but was getting array errors - working fine with your solution. – mmc501 Sep 20 '17 at 11:30
1

Yes you can using json_decode() function for example:

 $yourjson;/* your json */
 $events = json_decode($yourjson, true);
 foreach($events as $event){

 echo $event["isoDate"];
 }
M0ns1f
  • 2,705
  • 3
  • 15
  • 25
  • You should add the `true` parameter to `json_decode()` so that it makes it a php array instead of an stdClass. `json_decode($yourjson,true)`. More information on this can be seen on my answer here https://stackoverflow.com/questions/44893673/adressing-php-array-decoded-from-json/44893702#44893702 – GrumpyCrouton Sep 15 '17 at 16:24
  • Actually, `stdClass` would be perfectly fine. Just remeber to modify the objects with `->`. After that you even can encode it back, having the same array/object structure. – Fabian N. Sep 15 '17 at 16:32
  • @FabianN. yes that true – M0ns1f Sep 15 '17 at 16:33
  • Don't be afraid of `stdClass`! Use it, it is there for a reason. See my answer. – Fabian N. Sep 16 '17 at 09:06
-2

You can use json_decode to decode the json object to php array then modify the array and encode it using json_encode

Jaya Parwani
  • 167
  • 1
  • 6