JSON uses--php to define a literal object using the JSON data format

Source: Internet
Author: User
Tags parse error

Currently, the literal naming method is not supported in PHP.

The small partners at the front end know that the method of defining an object in JS with literal literals can be as follows:

var o = {' name ': ' Tom ', ' url ': ' Www.baidu.com ' };alert (o.name);

But in PHP this defines the object:

$o = {"Name": "Tom", "url": "Www.baidu.com"  };

The error will be

Parse error: syntax error, unexpected '{' in D:\server\htdocs\xm\data\04_product_ select.php on line 9

So when you need to use JSON, you can try the form of PHP array, and less use of the form of objects, it can be simpler and more intuitive (at present my point of view, perhaps not enough objective, look treatise). Then use the Json_enconde ($arr), the JSON format encoding, the method is as follows

$arr = [      ' name ' = ' Tom ',      ' age ' =        ]; $json = Json_encode ($arr);

When I consulted the Nanyi teacher's blog article " using JSON in PHP ", I saw what Mr. Ruan said, "JavaScript does not support associative arrays " (see). But did not find the information to refute him, or refute himself, so posted here, hope to be able to solve their own in the future, but also hope that you can guide twos, greatly appreciated!

But the last word:Json_enconde () converts an indexed array (indexed array) to array format only, and the associative array (associative array) to the object format. This feature should be kept in mind!

JSON uses--php to define a literal object using the JSON data format

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.