这次给大家带来php怎样读取本地的json文件,php读取本地json文件的注意事项有哪些,下面就是实战案例,一起来看一下。
1.data.json文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | {
"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文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php
echo "获取页面传来的参数" ;
$type = $_GET [ 'type' ];
$proId = $_GET [ 'id' ];
echo $type . "产品type" ;
echo $proId . "产品Id" ;
$json_string = file_get_contents ( 'json/data.json' );
$data = json_decode( $json_string , true);
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 );
?>
|
登录后复制
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
PHP怎样生成随机数
在Docker中搭建PHP开发环境
以上就是php怎样读取本地的json文件的详细内容,更多请关注php中文网其它相关文章!