1

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

], .....

asked Jul 5, 2014 at 5:14
0

1 Answer 1

1
 // 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;
}
answered Jul 5, 2014 at 7:41
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.