Blogger Information
Blog 142
fans 5
comment 0
visits 130031
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php读取本地json文件的实例
php开发大牛
Original
848 people have browsed it

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);
?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post