Php json_encode implementation and Chinese garbled code solution

Source: Internet
Author: User
Tags mysql tutorial urlencode

$ Arr = array ('a' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5 );

Echo json_encode ($ arr );

// Result

// {"A": 1, "B": 2, "c": 3, "d": 4, "e": 5}

/*

The following describes a json_encode Chinese garbled problem.
The solution is to use the urlencode () function to process the following. Before json_encode, urlencode () is used to process all the content in the array, and json_encode () is used to convert the content into a json string, finally, use urldecode () to convert the encoded Chinese characters back.
*/

Function arrayrecursive (& $ array, $ function, $ apply_to_keys_also = false)
{
Static $ recursive_counter = 0;
If (++ $ recursive_counter> 1000 ){
Die ('possible deep recursion attack ');
    }
Foreach ($ array as $ key => $ value ){
If (is_array ($ value )){
Arrayrecursive ($ array [$ key], $ function, $ apply_to_keys_also );
} Else {
$ Array [$ key] = $ function ($ value );
        }
 
If ($ apply_to_keys_also & is_string ($ key )){
$ New_key = $ function ($ key );
If ($ new_key! = $ Key ){
$ Array [$ new_key] = $ array [$ key];
Unset ($ array [$ key]);
            }
        }
    }
$ Recursive_counter --;
}


 
/*************************************** ***********************
 *
* Convert an array to a json string (compatible with Chinese characters)
* @ Param array $ array the array to be converted
* @ Return string the converted json string
* @ Access public
 *
**************************************** *********************/

Function json ($ array ){
Arrayrecursive ($ array, 'urlencode', true );
$ Json = json_encode ($ array );
Return urldecode ($ json );
}

$ Array = array
       (
'Name' => 'chia ',
'Age' => 20
);


Echo json ($ array );

// Application instance

$ Servname = "localhost ";
$ Sqlservname = "root ";
$ Sqlservpws = "123456 ";
$ Sqlname = "lock1 ";
$ Db = mysql tutorial _ connect ($ servname, $ sqlservname, $ sqlservpws) or die ("database tutorial connection failed ");
Mysql_select_db ($ sqlname, $ db );
$ SQL = "select * from t_operater ";
$ Result = mysql_query ($ SQL );
$ Rows = mysql_num_rows ($ result );
While ($ obj = mysql_fetch_object ($ result ))
{
$ Arr [] = $ obj;
   
}
Echo '({"total": "'. $ rows. '", "results":'. json_encode ($ arr ).'})';

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.