{
"AFL Round 16":{
"4166082":{
"EventID":4166082,
"ParentEventID":3744759,
"MainEvent":"North Melbourne v Hawthorn",
"OutcomeDateTime":"2014-07-06 02:00:00",
"Competitors":{
"Competitors":[
{
"Team":"Hawthorn To Win 40+",
"Win":"3.00"
}
],
"ActiveCompetitors":1,
"TotalCompetitors":1,
"HasWinOdds":true
},
"EventStatus":"Open"
},
"4167064":{
"EventID":4167064,
"ParentEventID":3744759,
"MainEvent":"North Melbourne v Hawthorn",
"OutcomeDateTime":"2014-07-06 02:00:00",
"Competitors":{
"Competitors":[
{
"Team":"Hawthorn (-5.5)",
"Win":"1.86"
},
{
"Team":"North Melbourne (+5.5)",
"Win":"1.86"
}
],
"ActiveCompetitors":2,
"TotalCompetitors":2,
"HasWinOdds":true
},
"EventStatus":"Open"
}
}
}
I am parsing json objects using PHP and here is a sample of my json. Everything is working fine. I just want to check if object property/value exists if yes then throw errors for example i want to check EventID, ParentEventID, OutcomeDateTime, Team (inside Competitors array) are valid property name and they are not null.
This is few lines of my code.
$SortedByDate = array();//Sorted By Date Array i.e key=EventID and value=OutcomeDateTime
//Accessing Root Element0
foreach ($json_a as $root_element => $childnode) {
//Accessing child elements
foreach( $childnode as $cKey => $subChild) {
$OutcomeDateTime_UTC=gmdate('Y-m-d H:i:s', strtotime($subChild['OutcomeDateTime']));
//checking ParentEventID=0 , Competitors array = 2 and OutcomeDateTime is greater than current time + 10 min
if($subChild['ParentEventID']=='0' and is_array($subChild['Competitors']['Competitors']) and count ($subChild['Competitors']['Competitors']) == 2 and $OutcomeDateTime_UTC>=$NewDateTime and !preg_match('/\(Live\)/',$subChild['MainEvent']) ) {
//Inserting values into array
$SortedByDate[$cKey] = $subChild['OutcomeDateTime'];;
}
}
}
I tired to add if(isset($subChild['OutcomeDateTime']) || is_null($subChild['OutcomeDateTime']))
to check if property name is OutcomeDateTime and it is not null and change json proerty's value (OutcomeDateTime) to null but i get an error that "Invalid argument supplied for foreach()"
is there a better way to check property/values before parsing???