2017-03-09 101 views
0

下面的代碼給出了一個帶有json對象的數組。但是我需要該數組的json字符串,以便以後可以使用json_decode。怎麼做?如何從對象數組中獲取json字符串

for($i=0;$i<5;$i++) 
{ foreach($allGames as $game) 
    { if($game['desc']==$sortGames[$i]) 
     { $text[$i]=array('Game_name'=>$game['desc'],'GameId'=>$game['gameId'],'order'=>$goalids,'length'=>$game['length']); 
      break; 
     } 
    } 
} 
$json_string=json_encode($text); 

$ json_string的值如下:

[{"Game_name":"a","GameId":"1697","order":["11022","11021","11020","11024","11023"],"length":"2.08783938975344"},{"Game_name":"b","GameId":"1800","order":["12196","12197","12194","12195","12193","12198"],"length":"1.16970835124072"}] 
+0

什麼ü平均排列的JSON字符串?在結果中你已經得到字符串 – bxN5

+0

結果是json數組。 json_decode無法解碼它。 – LSG

+0

所以問題是你不能解碼編碼的字符串? – bxN5

回答

1
for($i=0;$i<5;$i++) 
{ foreach($allGames as $game) 
    { if($game['desc']==$sortGames[$i]) 
     { $text[$i]=(array('Game_name'=>$game['desc'],'GameId'=>$game['gameId'],'order'=>json_decode($goalids),'length'=>$game['length'])); 
      break; 
     } 
    } 
} 
$json_string=json_encode($text); 
+0

的區別是什麼? – LSG

+0

json_decode()用於訂單 –

+0

好吧,它正在解決我的問題,但它創建訂單爲空。 – LSG

相關問題