Json使用php获取子元素

时间:2014-06-19 04:07:25

标签: php json cakephp-2.0

我来自.Net背景,我没有要求任何代码我只需要一个建议,以便我可以拉动我的子节点。这是我的JSON

{
   "State of Origin 2014":{
      "1471137":{
         "EventID":1471137,
         "ParentEventID":1471074,
         "MainEvent":"State Of Origin Series 2014",
         "OutcomeDateTime":"2014-06-18 20:10:00.0000000",
         "Competition":"State of Origin 2014",
         "Competitors":{
            "ActiveCompetitors":3,
            "Competitors":[
               {
                  "Team":"New South Wales (2 - 1)",
                  "Win":"2.15"                 
               },
               {
                  "Team":"New South Wales (3 - 0)",
                  "Win":"3.05"                
               },
               {
                  "Team":"Queensland (2 - 1)",
                  "Win":"3.30"
               }
            ],
            "TotalCompetitors":3,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      },
      "1471074":{
         "EventID":1471074,
         "ParentEventID":0,
         "MainEvent":"State Of Origin Series 2014",  
         "OutcomeDateTime":"2014-07-09 20:10:00.0000000",
         "Competition":"State of Origin 2014",
         "Competitors":{
            "ActiveCompetitors":2,
            "Competitors":[
               {
                  "Team":"New South Wales",
                  "Win":"1.33"             
               },
               {
                  "Team":"Queensland",
                  "Win":"3.30"
               }
            ],
            "TotalCompetitors":2,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      }
   },
   "State of Origin 2014 Game 2":{
      "3608662":{
         "EventID":3608662,
         "ParentEventID":3269132,
         "MainEvent":"New South Wales v Queensland",
         "Competitors":{
            "ActiveCompetitors":39,
            "Competitors":[
               {
                  "TeamName":"New South Wales 6-10",
                  "Win":"4.70"              
               },
               {
                  "TeamName":"Queensland 91+",
                  "Win":"201.00"                
               }
            ],
            "TotalCompetitors":39,
            "HasWinOdds":true
         },
         "EventStatus":"Open",
         "IsSuspended":false,
         "AllowBets":true
      }

   }
}

这就是我所做的。我能够获得root元素,现在我想要达到两个级别来抓住EventID和OutcomeDateTime。

PHP代码

$json_a=json_decode($string,true);
foreach ($json_a as $root_element => $childnode) {
    echo($childnode['EventID']);
      echo($childnode['OutcomeDateTime']);
      echo "<br>";
}

如果有人可以给我一个方法来降低两个级别,以便我可以处理我的其他脚本。我以前发过这个问题,但我没有得到很好的回应,可能是我无法以更好的方式表达我的问题,许多人认为我要求完整的源代码。这次我只需要一种方法来抓取子元素。 感谢

2 个答案:

答案 0 :(得分:0)

试试这个

$json_a=json_decode($string,true);

foreach ($json_a as $root_element => $childnode) {

    foreach( $childnode as $cKey => $subChild) {
        echo $subChild['EventID'];
        echo $subChild['OutcomeDateTime'];
        // To check whether the Competitors is an array and array count is > 0
        if (is_array($subChild['Competitors']['Competitors']) and count ($subChild['Competitors']['Competitors']) > 0 ) {
            foreach($subChild['Competitors']['Competitors'] as $compKey => $compVal) {
            // Since your array contains Team and TeamName , I use array_key_exists to check whick key to use
             $teamName = array_key_exists('Team',$compVal) ? $compVal['Team'] :  $compVal['TeamName'];
             $win      = $compVal['Win'];
             echo $teamName;
             echo $win;
         }
        }

    }

}

您必须执行嵌套foreach才能获得所需的输出

此外,由于echo是一种语言构造而非函数,因此(语句中不必使用) echo大括号

答案 1 :(得分:0)

你可以使用递归搜索方法搜索它:

function find_by_key($needle,$array) {
    foreach($array as $key=>$value) {
        $current_key=$key;
        if($needle===$key OR (is_array($value) && find_by_key($needle,$value) !== false)) {
            return $value;
        }
    }
    return false;
}

,主要代码应该是这样的

$arr=json_decode($string,true);
$EventID = find_by_key('EventID',$arr);
$OutcomeDateTime= find_by_key('OutcomeDateTime',$arr);