0

I am from .Net background and I AM NOT ASKING FOR ANY CODE I just need a suggestion so that i can pull my child nodes. This is my 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
      }

   }
}

This is what i have done. I am able to get root element and now i want to go two levels down to grap EventID and OutcomeDateTime.

PHP code

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

If anyone can just give me a way to get two levels down so that i can work on my other scripts. I had posted this question before but i was not welcomed with good responses may be i was not able to express my question in a better way and many thought i am asking for full source code. This time i just need a method to grap child elements. THanks

user3754680
  • 73
  • 1
  • 1
  • 6
  • which child exactly you are about to pull out, so that can help you out – shatheesh Jun 19 '14 at 04:13
  • The `$childnode` in this case is actually ` "State of Origin 2014":` so if you wish to get the properties of it's direct child, then you should try doing `$childnode[0]['EventID']` – Ohgodwhy Jun 19 '14 at 04:13
  • I am getting undefined offset 0 error and i want to get eventid, outcomedatetime and from competitors array team and win – user3754680 Jun 19 '14 at 04:17

2 Answers2

0

Try this

$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;
         }
        }

    }

}

You have to do a nested foreach to achieve your desired output

Also since echo is a language construct and not a function it is not mandatory to use ( ) braces in echo statement

shatheesh
  • 633
  • 6
  • 10
  • I am getting results but also getting fatal error: cannot use object of type stdclass as array on line 7 – user3754680 Jun 19 '14 at 04:25
  • @user3754680 then it's `$subChild->EeventID` or `as (array)$subChild` then `$subChild['EventID'];` – Ohgodwhy Jun 19 '14 at 04:27
  • double check whether you are properly using `json_decode($string,true)` with `true` as second parameter, otherwise the output will be an object only – shatheesh Jun 19 '14 at 04:27
  • I got it its fine now and how can i get competitors array team and win value?in some data team is replaced with teamname – user3754680 Jun 19 '14 at 04:27
  • Try the same nested `foreach` style and let me know incase of difficulties – shatheesh Jun 19 '14 at 04:28
  • @user3754680 I have updated the answer to get competitors array. If my answer is helpful do accept it – shatheesh Jun 19 '14 at 04:41
  • Thanks shatheesh i just only need competitors value if its array count is equal to 2 – user3754680 Jun 19 '14 at 04:52
  • One more question is there any way to sort the json data by outcomedatetime and just grap first 3 records? – user3754680 Jun 19 '14 at 05:18
  • you can use `array_multisort` on array. check this [http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php](http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php) – shatheesh Jun 19 '14 at 08:25
0

you can search it down using recursive search method:

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;
}

and the main code should be like this

$arr=json_decode($string,true);
$EventID = find_by_key('EventID',$arr);
$OutcomeDateTime= find_by_key('OutcomeDateTime',$arr);
kangaswad
  • 765
  • 6
  • 12