How PHP reads the local JSON file

Source: Internet
Author: User
Tags php development environment
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

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.