JSON按键排序,位于第二级

时间:2014-06-19 06:17:47

标签: php sorting cakephp json

嗨有没有办法在解析之前对JSON进行排序?我想按OutcomeDateTime排序。这是我的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
      }

   }
}

这是我的代码

foreach ($json_a as $root_element => $childnode) {
  echo("{<br />");
    foreach( $childnode as $cKey => $subChild) {

      if($subChild['ParentEventID']=='0')
      {

       echo("Australian Rules : [");
         echo("<br />");
         echo("{<br />");
        echo "MainEvent: ".$subChild['MainEvent'];
        echo "<br />OutcomeDateTime: ".$subChild['OutcomeDateTime'];
         echo "<br />Competitors:[ <br />{";

        if (is_array($subChild['Competitors']['Competitors']) and count ($subChild['Competitors']['Competitors'])  == 2 ) {
            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['Name'];
             $win      = $compVal['Win'];
              echo "<br />Team: ".$teamName;
              echo "<br />Win: ".$win;
               echo "<br /> } , {";
         }
        }
}
    }

我想通过OutcomeDateTime对数据进行排序,但最新数据应为Time.Now + 10minutes。我的排序元素在二级内。有没有办法按OutcomeDateTime排序并获得前3名结果?

0 个答案:

没有答案