I would like the JSON data to go thought a foreach loop in php but every time I try to do it I get this error message:
Catchable fatal error: Object of class stdClass could not be converted to string
PHP:
<?php
$xml_events = simplexml_load_file('xml_data/current_events.xml');
$results = array();
foreach ($xml_events->event as $event) {
array_push($results, array(
'name' => $event->name,
'cover' => $event->cover,
));
}
$data = json_encode($results);
$json = json_decode($data);
foreach ($json as $name)
{
echo $name->name;
}
XML:
<?xml version="1.0"?>
<data>
<event>
<name>Barclays ATP World Tour Finals</name>
</event>
</data>
JSON Data:
"[{"name":{"0":"Barclays ATP World Tour Finals"}},{"name":{"0":"2015 National Television Awards"}},{"name":{"0":"Barclays ATP World Tour Finals"}},{"name":{"0":"Barclays ATP World Tour Finals"}},{"name":{"0":"Barclays ATP World Tour Finals"}},{"name":{"0":"Doctor who"}},{"name":{"0":"Apple Party"}},{"name":{"0":"ice-cream"}}]"
$name is an object, so the solution is to use:
echo $name->name->{0};