Home  >  Article  >  Backend Development  >  php json_encode

php json_encode

WBOY
WBOYOriginal
2016-08-08 09:27:121262browse

PHP json_encode() function introduction


Using the json_encode() built-in function in php (php > 5.2) can be used. The data in php can be transferred well to other languages ​​​​and used.

The function of this function is to convert numerical values ​​into json data storage format.

01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php

02<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none> <codecourier new min-height:inherit color:rgb background:none>$arr <codecourier new min-height:inherit background:none>= <codecourier new font-weight:bold min-height:inherit color:rgb background:none>array</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p> </p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>03<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>                                                    <p></p> <table><tbody> <tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>04<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>                                                                                                                                    <codecourier new min-height:inherit color:blue background:none><codecourier new min-height:inherit background:none><codecourier new min-height:inherit color:blue background:none>05<codecourier new min-height:inherit background:none></codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr> 'Age'</tbody></table>=>22<p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none><tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>06<codecourier new min-height:inherit color:blue background:none><codecourier new min-height:inherit background:none> );</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>07<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none> </tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>08<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>$jsonencode <codecourier new min-height:inherit background:none>= json_encode(<codecourier new min-height:inherit color:rgb background:none>$arr<codecourier new min-height:inherit background:none>);</codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>09<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>echo <codecourier new min-height:inherit color:rgb background:none>$jsonencode<codecourier new min-height:inherit background:none>;</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p></p> <table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>10<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>?> 1</codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>{"Name"<p> </p>:null,<divcourier new min-height:inherit><p>"Age"</p> <p></p> <table>:20}<tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none><tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>json_encode function Chinese is encoded as null, Google it, it's very simple, in order to closely integrate with the front end, Json only supports utf -8 encoding, I think the reason is that the front-end Javascript is also utf-8. <codecourier new min-height:inherit color:blue background:none><codecourier new min-height:inherit background:none><codecourier new min-height:inherit color:blue background:none><codecourier new min-height:inherit background:none>1</codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody> </table><?php</p></p><p></p><divCourier New',Courier,monospace!important; min-height:inherit!important"></p><p><table><tbody><tr><tdCourier New',Courier,monospace!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:top!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:3em!important; line-height:1.1em!important; min-height:inherit!important; color:rgb(175,175,175)!important; background:none!important"><codeCourier New',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">2$array array

3    (

4 ,'your sister' ),5

6    );

7 

8echo json_encode($array);

9?> 1

{

"title "

:

"

u6d63u72b2ue785","body":"men"}<span><span></span><br> <spanCourier New',Courier,monospace; line-height:13.1999998092651px; text-indent:24px; background-color:rgb(248,248,248)">iconv uses GBK when converting formats, and gb2312 reports an error because GB2312 only contains 6763 Chinese characters. When uncollected Chinese characters appear, iconv reports an error. GBK is fully backward compatible with GB2312-80 and supports more characters, but errors will still occur. The solution is to use GB18030. GB18030 is basically compatible with GBK and expands more than a hundred fonts. </span></p></p></p><p>All Chinese characters in the array are missing or u6d63 etc. appear after json_encode. </p><p>The solution is to use the urlencode() function to process the following. Before json_encode, use urlencode() to process all the contents of all arrays, then use json_encode() to convert it into a json string, and finally use urldecode() to encode it. Transfer the Chinese that you have passed back. </p><divCourier New',Courier,monospace!important; min-height:inherit!important"><p><p><table><tbody><tr><tdCourier New',Courier,monospace!important; border:0px!important; outline:0px!important; float:none!important; vertical-align:top!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:3em!important; line-height:1.1em!important; min-height:inherit!important; color:rgb(175,175,175)!important; background:none!important"><codeCourier New',Courier,monospace!important; min-height:inherit!important; display:block!important; background:none!important">01<?php

02 /************** ************************************************

03 *

04* Use a specific function to process all elements in the array

05 * @param string &$array The string to be processed

06* @param string $function The function to be executed

07* @return boolean $apply_to_keys_also Whether to also apply to keys

08 * @access public

09*

10 *************************************************************/

11function arrayRecursive(&$array$function$apply_to_keys_also = false)

12{

13    static $recursive_counter = 0;

14    if (++$recursive_counter > 1000) {

15        die('possible deep recursion attack');

16    }

17    foreach ($array as $key => $value) {

18        if (is_array($value)) {

19            arrayRecursive($array[$key], $function$apply_to_keys_also);

20    &nbs
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:A "pit" in PHPNext article:A "pit" in PHP