{
"goods":[
{
"type":1,
"name":"wow精選",
"product":[
{
"id":98,
"name":"真皮大衣",
"title":"單桶原釀酒 威士忌 新春禮盒 限量獨家",
"titleDesc":"蘇格蘭麥芽糖,中國定制版",
"price":1298.00
},
{
"id":99,
"name":"品牌內(nèi)衣",
"title":"單桶原釀酒 威士忌 新春禮盒 限量獨家222",
"titleDesc":"蘇格蘭麥芽糖,中國定制版222",
"price":1298.00
}
]
},
{
"type":2,
"name":"特惠商品",
"product":[]
}
]
}
?php
echo "獲取頁面?zhèn)鱽淼膮?shù)";
$type = $_GET['type'];
$proId = $_GET['id'];
echo $type."產(chǎn)品type";
echo $proId."產(chǎn)品Id";
// 從文件中讀取數(shù)據(jù)到PHP變量
$json_string = file_get_contents('json/data.json');
// 用參數(shù)true把JSON字符串強制轉(zhuǎn)成PHP數(shù)組
$data = json_decode($json_string, true);
// 顯示出來看看
// var_dump($json_string);
// var_dump ($data);
// print_r($data);
//產(chǎn)品循環(huán)
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);
?>