Solve the garbled problem of gbk passed to JavaScript and json_encode through PHP arrays. This article introduces how to solve gbk garbled characters transmitted by PHP arrays to JavaScript and json_encode. The following describes how to create a JSON function, the following is a copy of a Daxia code from the internet. This article introduces how to solve the gbk Chinese garbled characters that PHP arrays pass to JavaScript and json_encode. below is the creation of JSON Functions, this section is from a man on the Internet
The code is as follows: |
|
/*************************************** *********************** * * Use a specific function to process all elements in the array * @ Param string & $ array the string to be processed * @ Param string $ function the function to be executed * @ Return boolean $ apply_to_keys_also whether it is also applied to the key * @ Access public * **************************************** *********************/ Function arrayRecursive (& $ array, $ function, $ apply_to_keys_also = false) { 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]); } } } } /*************************************** *********************** * * 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 ); } |
Database Connection value to array $ array1
The code is as follows: |
|
$ Dbcnx = @ mysql_connect ("localhost", "root", "1234 "); If (! $ Dbcnx ){ Echo ("Unable to connect to the". "database server at this time ."); Exit (); } If (! @ Mysql_select_db ("pms ")){ Echo ("Unable to locate the joke". "database at this time ."); Exit (); } Mysql_query ("set names 'gb2312 '"); $ Q = mysql_query ("select * from ability where ALV = 1 "); While ($ row = mysql_fetch_array ($ q )){ $ Array1 [] = $ row [AName]; } |
Array array1 is passed to JavaScript to array ability1