{
"goods":[
{
"type":1,
"name":"wow精選",
"product":[
{
"id":98,
"name":"真皮大衣",
"title":"單桶原釀酒 威士忌 新春禮盒 限量獨家",
"titleDesc":"蘇格蘭麥芽糖,中國定制版",
"price":1298.00
},
{
"id":99,
"name":"品牌內衣",
"title":"單桶原釀酒 威士忌 新春禮盒 限量獨家222",
"titleDesc":"蘇格蘭麥芽糖,中國定制版222",
"price":1298.00
}
]
},
{
"type":2,
"name":"特惠商品",
"product":[]
}
]
}
?php
echo "獲取頁面傳來的參數";
$type = $_GET['type'];
$proId = $_GET['id'];
echo $type."產品type";
echo $proId."產品Id";
// 從文件中讀取數據到PHP變量
$json_string = file_get_contents('json/data.json');
// 用參數true把JSON字符串強制轉成PHP數組
$data = json_decode($json_string, true);
// 顯示出來看看
// var_dump($json_string);
// var_dump ($data);
// print_r($data);
//產品循環
function foreachFun($d,$type,$proId)
{
foreach ($d["goods"] as $key => $value) {
if($value["type"] == $type){
$results = $value;
}
}
foreach ($results["product"] as $key => $value) {
if($value["id"] == $proId){
$result = $value;
}
}
return $result;
}
$res = foreachFun($data,$type,$proId);
print_r($res);
?>