2014-07-07 119 views
1

我使用PHP解析Json。這裏是我的代碼的一小部分PHP Json輸出格式

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

我的輸出

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

但不是TEAM1,贏1,2隊,WIN2我想A隊,贏得這樣的。我只想使用字母A,B而不是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" 
}  

任何人都可以告訴我我在找什麼?

+0

那麼做一個26數組對象,並用值的索引來替換。但我不能分類爲什麼你想要這個設施? –

回答

1

你進入for循環之前,初始化$countTeamA

$countTeam = "A"; 

和移動$countTeam++;到的結束for循環。

+0

其實我是TeamB,TeamC不是TeamA,TeamB。因爲計數器是++,因此它正在打印B,C。在整數中是否有任何類似的0,以便它可以打印1,2 – user3754380

+0

@ user3754380哦,您應該將'$ countTeam ++;'移動到循環的最後一行 – meda