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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题