PHP讀取Json檔案
1.data.json檔案
{
"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":[]
}
]
}
2.php檔案
<?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);
?>
---------------------
作者:milli236
來源:CSDN
原文:https://blog.csdn.net/milli236/article/details/79031743
版權宣告:本文為博主原創文章,轉載請附上博文連結!