Example of php reading local json files,

Source: Internet
Author: User

Example of php reading local json files,

1. data. json File

{"Goods": [{"type": 1, "name": "wow selected", "product": [{"id": 98, "name ": "leather coat", "title": "exclusive single-bucket original liquor whisky Chinese New Year gift box Limited", "titleDesc": "Scotland maltose, Chinese customized version", "price": 1298.00 }, {"id": 99, "name": "brand underwear", "title": "exclusive 222 New Year gift box for single bucket original liquor whisky Limited", "titleDesc": "Scotland maltose, customized Chinese Version 222 "," price ": 1298.00}] },{" type ": 2," name ":" special offer "," product ": []}

2. php file

<? Php echo "GET parameters from the page"; $ type = $ _ GET ['type']; $ proId = $ _ GET ['id']; echo $ type. "product type"; echo $ proId. "Product Id"; // The variable $ json_string = file_get_contents ('json/data. json '); // use the true parameter to forcibly convert the JSON string to a PHP array $ data = json_decode ($ json_string, true); // display it 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);?>

The above example of php reading a local json file is all the content shared by the editor. I hope you can give us a reference and support the help house.

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.