1

I am parsing Json using PHP. Here is a small part of 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']))
                        );

  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;

My output is

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

      "Team1": "East Fremantle",
      "Win1": "1.22",

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

      "Team1": "East Perth",
      "Win1": "1.12",

      "Team2": "Peel Thunder",
      "Win2": "6.00"
}

But instead of Team1 , Win 1, Team 2, Win2 i want Team A, Win A like that. I just want to use alphabets A, B rather than 1, 2.

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

      "TeamA": "West Perth",
      "WinA": "1.57",

      "TeamB": "Swan Districts",
      "WinB": "2.35"


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

      "TeamA": "East Fremantle",
      "WinA": "1.22",

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

      "TeamA": "East Perth",
      "WinA": "1.12",

      "TeamB": "Peel Thunder",
      "WinB": "6.00"
}    

Can anyone tell me what i am missing??

user3754380
  • 651
  • 2
  • 7
  • 15
  • Well make a 26 array object and replace with the index of values.But i can't categorized why you want this facility? – Just code Jul 07 '14 at 02:43

1 Answers1

1

Before you enter the for loop , initialize $countTeam to A:

$countTeam = "A";

and move $countTeam++; to the end of the for loop.

meda
  • 45,103
  • 14
  • 92
  • 122
  • actually I am getting TeamB,TeamC not TeamA, TeamB. Because counter is ++ so it is printing B,C. Is there anything like 0 in integers so that it prints 1,2 – user3754380 Jul 07 '14 at 02:49
  • @user3754380 oh you should move `$countTeam++;` to the last line of the loop – meda Jul 07 '14 at 02:50