user3754380
user3754380

Reputation: 701

PHP JSON parsing formatting

I am parsing JSOn using PHP. Right now my json is working fine i just want to format my output. I have created a rootobj array where i am inserting few properties then inside that rootobj there is another array but i want inner array properties to be in the root element , This is my code

  $rootObj = array(
     MainEvent' => $event_value['MainEvent'],
    'OutcomeDateTime' => $formatteddate->format('Y-m-d H:i:s'),
    'OutcomeDateTimeUTC' => gmdate('Y-m-d H:i:s', strtotime($event_value['OutcomeDateTime']))
     );
    //inner array
    foreach($event_value['Competitors']['Competitors'] as $compKey => $compVal) {
    $teamName = array_key_exists('Team',$compVal) ? $compVal['Team'] :  $compVal['Name'];
    $win = $compVal['Win'];
    //Creating Competitor Array
     $CompetitorArray[] = array(
     "Team" => $teamName,
     "Win" => $win,
     );
     }
     $rootObj ['Competitors'] = $CompetitorArray;

This is a sample of my output

    ...
     "MainEvent": "West Perth v Swan Districts",
      "OutcomeDateTime": "2014-07-05 16:05:00",
       "OutcomeDateTimeUTC": "2014-07-05 06:05:00",
      "Competitors": [
        {
          "Team": "West Perth",
          "Win": "1.57"
        },
        {
          "Team": "Swan Districts",
          "Win": "2.35"
        }
      ]
    },
    {
      "MainEvent": "East Fremantle v Perth",
      "OutcomeDateTime": "2014-07-05 16:05:00",
       "OutcomeDateTimeUTC": "2014-07-05 06:05:00",
      "Competitors": [
        {
          "Team": "East Fremantle",
          "Win": "1.22"
        },
        {
          "Team": "Perth",
          "Win": "4.15"
        }
      ]
    },
    {
      "MainEvent": "East Perth v Peel Thunder",
      "OutcomeDateTime": "2014-07-05 16:05:00",
       "OutcomeDateTimeUTC": "2014-07-05 06:05:00",
      "Competitors": [
        {
          "Team": "East Perth",
          "Win": "1.12"
        },
        {
          "Team": "Peel Thunder",
          "Win": "6.00"
        }
      ]
    }
  ],
  .....

Bur i want my output to be like this

 ...
 "MainEvent": "West Perth v Swan Districts",
  "OutcomeDateTime": "2014-07-05 16:05:00",
   "OutcomeDateTimeUTC": "2014-07-05 06:05:00",

      "Team1": "West Perth",
      "Win1": "1.57",

      "Team2": "Swan Districts",
      "Win2": "2.35"


},
{
  "MainEvent": "East Fremantle v Perth",
  "OutcomeDateTime": "2014-07-05 16:05:00",
   "OutcomeDateTimeUTC": "2014-07-05 06:05:00",

      "Team3": "East Fremantle",
      "Win3": "1.22",

      "Team4": "Perth",
      "Win4": "4.15"
},
{
  "MainEvent": "East Perth v Peel Thunder",
  "OutcomeDateTime": "2014-07-05 16:05:00",
   "OutcomeDateTimeUTC": "2014-07-05 06:05:00",

      "Team5": "East Perth",
      "Win5": "1.12",

      "Team6": "Peel Thunder",
      "Win6": "6.00"
}

], .....

Upvotes: 1

Views: 58

Answers (1)

LoGary
LoGary

Reputation: 322

 // outside of scope
 $countTeam=0;

 ...



 foreach($event_value['Competitors']['Competitors'] as $compKey => $compVal) {
   $countTeam++;
   $teamName = array_key_exists('Team',$compVal) ? $compVal['Team'] :  $compVal['Name'];
   $win = $compVal['Win'];
  //Insert to root object
   $rootObj["Team".$countTeam] = $teamName;
   $rootObj["Win".$countTeam] = $win;

}

Upvotes: 1

Related Questions