This article mainly for you to share the PHP array into JSON format method, the need for friends can refer to this example
The example in this article describes how the PHP array is converted to JSON format. Share to everyone for your reference. The implementation method is as follows:
The code is as follows:
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 ea CH 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_numeric ($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;}