douao1959 2014-07-03 00:16
浏览 39
已采纳

PHP Json对象属性/值错误处理

{
   "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???

  • 写回答

2条回答 默认 最新

  • douke1942 2014-07-03 00:48
    关注

    Try this and see if it does what you mean. If not, I don't understand. If it does solve your problem I'll explain why...

    //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' && is_array($subChild['Competitors']['Competitors']) && count ($subChild['Competitors']['Competitors'])  == 2 && $OutcomeDateTime_UTC>=$NewDateTime && !preg_match('/\(Live\)/',$subChild['MainEvent']) ) {
                //Inserting values into array
                $SortedByDate[$cKey] = $subChild['OutcomeDateTime'];
            }
            if(isset($subChild['OutcomeDateTime']) && !is_null($subChild['OutcomeDateTime'])) {
                $subChild['OutcomeDateTime'] = null;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?