首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP对象属性/值错误处理

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

Stack Overflow用户
提问于 2014-07-03 00:16:12
回答 2查看 136关注 0票数 0
代码语言:javascript
复制
{
   "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"
      }

  }
}

我正在使用PHP解析json对象,下面是我的json示例。一切都很好。我只想检查对象属性/值是否存在,如果是,那么抛出错误--例如,我想检查EventID、ParentEventID、OutcomeDateTime、Team (在竞争对手数组中)是有效的属性名,它们不是null。

这是我的代码的几行。

代码语言:javascript
复制
$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'];;
        }
    }
}

我尝试添加if(isset($subChild['OutcomeDateTime']) || is_null($subChild['OutcomeDateTime']))来检查属性名是否为OutcomeDateTime,并且它不是null,并将json的值(OutcomeDateTime)更改为空,但我得到了一个"Invalid argument supplied for foreach()"错误

是否有更好的方法在解析之前检查属性/值?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-03 00:48:26

试试这个,看看它是否符合你的意思。如果不是,我不明白。如果它解决了你的问题我会解释为什么..。

代码语言:javascript
复制
//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
EN

Stack Overflow用户

发布于 2014-07-03 00:43:17

我只想检查对象属性/值是否存在,如果是,那么抛出错误

您的措辞没有意义,也有点奇怪,也许您是说要验证每个键(如果存在的话),如果这些键的每个值都不是空的。

例如,我想检查EventID、ParentEventID、OutcomeDateTime、Team (在竞争对手数组中)是有效的属性名,它们不是null。

这是一把小提琴。尝试删除json字符串中的一些元素以检查:Fiddle

代码语言:javascript
复制
$json_a = '{ "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" } }}';
$json_a = json_decode($json_a, true);
$json_a = reset($json_a); // ignore these parts since you already know how to get them

$errors = array();
$valid_keys = array('EventID', 'ParentEventID', 'OutcomeDateTime', 'MainEvent', 'Competitors', 'EventStatus');
foreach($json_a as $event_id => $values) {
    // check for keys
    $keys = array_keys($values);
    foreach($valid_keys as $key) {
        if(!in_array($key, $keys)) {
            // check keys, not valid if it goes here
            $errors[] = "<b>$key</b> is missing on your data <br/>";
        } else {
            // valid keys, check values
            if(empty($values[$key])) {
                // empty values
                $errors[] = "<b>$key</b> has an empty value <br/>";
            }
        }
    }

    // next checking, competitors
    foreach($values['Competitors']['Competitors'] as $competitors) {
        if(empty($competitors)) {
            // if competitors is empty
            $errors[] = "<b>Competitors</b> has an empty value <br/>";
        }
    }
}

if(!empty($errors)) {
    // not a good error triggering device, just change this to something else
    trigger_error('<pre>'.implode($errors).'</pre>', E_USER_ERROR);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24543082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档