JSON array to normal array normal array to JSON array

Source: Internet
Author: User
Tags string format

1.json_decode ()

Json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

json_decode-encoding a JSON-formatted string

Description
Mixed Json_decode (String $json [, bool $assoc])
Accept a JSON-formatted string and convert it to a PHP variable

Parameters

Json
String in JSON string format to decode.

Assoc
When this argument is TRUE, an array is returned instead of an object.


return value
Returns an object or if the optional assoc parameter are TRUE, an associative array is instead returned.

Example of Json_decode ()

<?  $json = ' {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4, ' E ': 5} 'var_dump(Json_decode ($json  var_dump(Json_decode ($jsontrue

Results

Object (StdClass) #  ["a"] = Int (1) ["B"] = + int (2) ["C"] = + int (3) ["D"] + int (4) [ "E"] = + int (5Array(5) {["a"] = + int (1) ["b"] = = Int (2) ["C"] = > int (3) ["D"] = + int (4) ["E"] + int (5

You can see an associative array of Json_decode ($data, true) output, so Json_decode ($data) outputs an object, and Json_decode ("$arr", true) is forcing it to generate a PHP associative array.

2.json_encode ()

Json_encode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)

json_encode-JSON Encoding of variables

Report a bug description
String Json_encode (mixed $value [, int $options = 0])
Returns the JSON form of value values

Report a bug parameter

Value
The value to encode, in addition to the resource type, can be any data type

This function can only accept UTF-8 encoded data.

Report a bug return value
A successful encoding returns a string in JSON form or FALSE on failure.

Json_encode () Example

<?  $arrarray (' A ' =>1, ' B ' =>2, ' C ' =>3, ' d ' =>4, ' e ' =>5Echo Json_encode ($arr?>

Results

As you can see, Json_encode () and Json_decode () are compiled and deserialized, and note that JSON only accepts UTF-8 encoded characters, so the parameters of Json_encode () must be utf-8 encoded, otherwise they will be null or empty.

The transcoding format uses the following function

<? PHP $arr=iconv(' GBK ', ' utf-8 ',$arr);

JSON array to normal array normal array to JSON array

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.