This time for everyone to bring PHP how to read the local JSON file, PHP read the local JSON file considerations are what, the following is the actual case, take a look.
1.data.json file
{"Goods": [{"Type": 1, "name": "Wow featured", "Product": [{"id": 98, "name": "Leather Coat", "title": "Single barrel original whisky Spring gift Box Limited exclusive", "Titledesc": " Scottish maltose, China Custom Edition "," Price ": 1298.00},{" id ":" The Name ":" Brand Underwear "," title ":" Single barrel original whisky Spring gift Box Limited exclusive 222 "," Titledesc ":" Scottish maltose, China custom version 222 "," Price ": 1298.00}]},{" type ": 2," name ":" Special Merchandise "," Product ": []}]}
2.php file
<?php echo "Get the parameters from the page"; $type = $_get[' type ']; $proId = $_get[' id ']; echo $type. " Product Type "; echo $proId. " Product ID "; Reading data from a file to a PHP variable $json _string = file_get_contents (' Json/data.json '); Use the parameter true to force the JSON string into the PHP array $data = Json_decode ($json _string, true); Show up to 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);?>
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
How PHP generates random numbers
Build a PHP development environment in Docker