PHP array conversion to JSON string program code _php Tutorial

Source: Internet
Author: User
Data Conversion JS Format data is a common method of data transfer, especially as Ajax will often use to convert the data into JSON and then converted back, see an example below.
The code is as follows Copy Code

function Array_to_json ($array) {
if (! Is_array ($array)) {
return false;
}

$associative = Count (Array_diff (Array_keys ($array), Array_keys (Array_keys ($array)));
if ($associative) {

$construct = Array ();
foreach ($array as $key = = $value) {

We first copy each key/value pair into a staging array,
Formatting each key and value properly as we go.


//Format the key:
if (Is_numeric ($key)) {
$key = "Key_$key";
}
$key = "". Addslashes ($key). """;

//Format The value:
if (Is_array ($value)) {
$value = Array_to_json ($value);
} else if (! is_n Umeric ($value) | | Is_string ($value)) {
$value = "". Addslashes ($value). """;
}

//ADD to staging array:
$construct [] = "$key: $value";
}

//Then we collapse the staging array into the JSON form:
$result = "{". Implode (",", $construct). "}";

} else {//If the array is a vector (not associative):


$construct = Array ();
foreach ($array as $value) {

Format The value:
if (Is_array ($value)) {
$value = Array_to_json ($value);
} else if (! Is_numeric ($value) | | is_string ($value)) {
$value = "". Addslashes ($value). """;
}

Add to Staging array:
$construct [] = $value;
}

Then we collapse the staging array into the JSON form:
$result = "[". Implode (",", $construct). "]";
}

return $result;
}

You can try this, and json_encode change to the above function to see if it's normal?

The code is as follows Copy Code

if ($_get[' enews ']== ' OK ') {
echo json_encode (Array (' a ' = ' Wangjin '); exit;
}
?>

About PHP in Json_encode

Json_encode () converts different types of PHP variables to the corresponding JSON string, string Json_encode (mixed $value [, int

$options = 0])

PHP 5.3.0

Json_hex_quot: Converts all double quotation marks (") to u0022.
Instance code:

The code is as follows Copy Code
$data = ' "';
echo Json_encode ($data); // """
Echo Json_encode ($data, Json_hex_quot);

"u0022" Json_hex_tag: Converts all greater than (>) to u003e,

All of the less than (<) is converted to u003c.
Json_hex_amp: Converts all the numbers (&) to u0026.
Json_hex_apos: Converts all single quotation marks (') to u0027.
Json_force_object: Forces the output to be a JSON object when value is a non-associative array. When the recipient requires the data to be an object and value is an empty array

Use.

Instance code:

The code is as follows Copy Code
$data = Array ();
echo Json_encode ($data); // []
Echo Json_encode ($data, json_force_object); // {}

PHP 5.3.3

Json_numeric_check:encodes NUMERIC strings as numbers.

PHP 5.4.0

Json_bigint_as_string:encodes large integers as their original STRING value. Available since PHP
5.4.0.

Json_pretty_print:use whitespace in returned data to format it. Available since PHP 5.4.0.
Json_unescaped_slashes:don ' t escape/. Available since PHP 5.4.0.
Json_unescaped_unicode:encode multibyte UNICODE characters literally (default is to escape as uxxxx).

Available since PHP 5.4.0.


http://www.bkjia.com/PHPjc/445290.html www.bkjia.com true http://www.bkjia.com/PHPjc/445290.html techarticle Data Conversion JS Format data is a common method of data transfer, especially as Ajax will often use to convert the data into JSON and then converted back, see an example below. Code such as ...

  • 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.