php reads an instance of a local json file
- 2021-09-12 00:33:39
- OfStack
1. data. json file
{
"goods":[
{
"type":1,
"name":"wow Select ",
"product":[
{
"id":98,
"name":" A leather coat ",
"title":" Single barrel original wine making Whiskey New Year Gift Box Limited exclusive ",
"titleDesc":" Scottish maltose, China customized version ",
"price":1298.00
},
{
"id":99,
"name":" Brand underwear ",
"title":" Single barrel original wine making Whiskey New Year Gift Box Limited exclusive 222",
"titleDesc":" Scottish maltose, China customized version 222",
"price":1298.00
}
]
},
{
"type":2,
"name":" Preferential merchandise ",
"product":[]
}
]
}
2. php file
<?php
echo " Get the parameters from the page ";
$type = $_GET['type'];
$proId = $_GET['id'];
echo $type." Products type";
echo $proId." Products Id";
// Read data from a file to PHP Variable
$json_string = file_get_contents('json/data.json');
// With parameters true Put JSON String forced to turn into PHP Array
$data = json_decode($json_string, true);
// Show it and see
// var_dump($json_string);
// var_dump ($data);
// print_r($data);
// Product cycle
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);
?>