PHP read the local JSON file instance sharing

Source: Internet
Author: User
This article is mainly for you to share a PHP read local JSON file instance, has a good reference value, I hope to help you. Follow the small series together to see it, hope to help everyone.


{"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"] = = $t     ype) {$results = $value;     }} foreach ($results ["Product"] as $key = + $value) {if ($value ["id"] = = $proId) {$result = $value;  }} return $result;  } $res = Foreachfun ($data, $type, $proId); Print_r ($res);; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.