我得到了这些 Json 数据:
[{"category":"Pizza","name":"Beef Pronto","desc":"Description of Beef Pronton here","price":"12"},
{"category":"Drink","name":"Cool Delight","desc":"Description of Coold Delight here","price":"5"},
{"category":"Drink","name":"Cola","desc":"Description of Cola","price":"4"}
]
使用 Javascript,我已成功管理数据以呈现如下:
比萨
-Beef Pronto:这里对 Beef Pronto 的描述:12
喝
-Cool Delight:Coold Delight 的描述:5
-可乐:可乐描述:4
任何想法如何用PHP做到这一点?
--> 好的,伙计们,这就是我使用 PHP 的方式:
<?
$listedmenuJSON = '[{"category":"Pizza","name":"Beef Pronto","desc":"Description of Beef Pronton here","price":"12"},
{"category":"Drink","name":"Cool Delight","desc":"Description of Coold Delight here","price":"5"},
{"category":"Drink","name":"Cola","desc":"Description of Cola","price":"4"}
]';
$json_decoded = json_decode($listedmenuJSON);
foreach ($json_decoded as $categoryvalue){
//echo $categoryvalue->category."<br/>";
$tempcategoryvalue[] = $categoryvalue->category;
$arrunique = array_unique($tempcategoryvalue);
}
foreach ($arrunique as $tmpcategory){
echo '<br/><b>'.$tmpcategory.'</b></br>';
foreach ($json_decoded as $tempo){
if($tempo->category == $tmpcategory){
echo $tempo->name.'<br/>';
echo '<i>'.$tempo->desc.'.......</i>';
echo $tempo->price.'<br/>';
}
}
}
?>
它将生成如下:
Pizza
Beef Pronto
这里是 Beef Pronto 的描述.......12
喝
Cool Delight
这里是Coold Delight 的描述.......5
可乐 可乐
的描述............ 4