如何从对象数组中获取json字符串

时间:2017-03-09 12:56:54

标签: php arrays json

以下代码给出了一个带有json对象的数组。但我需要该数组的json字符串,以便我以后可以使用json_decode。怎么做?

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

1 个答案:

答案 0 :(得分: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);