我得到了这些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:牛肉Pronton的描述:12
饮料
-Cool Delight:Coold Delight的描述:5
-Cola:可乐的描述: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/>';
}
}
}
?>
它将生成如下:
披萨
牛肉Pronto
这里的牛肉Pronton的描述....... 12
饮料
酷喜悦
这里的冷酷描述....... 5
可口可乐
可乐的描述....... 4
答案 0 :(得分:0)
如果你在PHP代码中使用json使用以下行将json转换为PHP数组并操纵数组以获得所需的输出
$array = json_decode($json,TRUE);
在javascript用户下面的代码中将你的json转换为数组。
var obj = jQuery.parseJSON(responce);
答案 1 :(得分:0)
尝试使用PHP函数'json_decode'(http://php.net/manual/en/function.json-decode.php)。这将在PHP中创建一个关联数组。访问和操作数据应该非常简单。
答案 2 :(得分:0)
如果您使用SQL服务器,则可以使用“按类别分组”
选择表格否则,您可以多次验证数组。
SQL:
<?php
$query = "SELECT * FROM 'table'
GROUP BY category
having (category=\"Pizza\");"
--SELECT * FROM 'table'
--GROUP BY category
--having (category="Drink");
如果你在php中使用它,你必须使用sql_query(“query”)