Array to JSON
<?php header ("Content-type: text/html; charset=utf-8"); $mydb = mysql_connect ("localhost", "root", "root"); if (! $mydb) { die (' could Not connect: '. mysql_error (); } $db _selected=mysql_select_db (" MySQL ", $mydb); //$sql = " Select * from person where lastname= " Adams ' "; $sql =" Select * from user "; $result =mysql_query ($sql, $mydb); //print_r (mysql_fetch_array ($result)); //Processing output array format // $db 1=mysql_query ("select * from ' Tb_info '"); /* $arr =array (); while ($rows = Mysql_fetch_array ($db 1)) { $key = $rows [' id '] $arr [$key] = $rows [' Qucount '] } ] /*********************************************************** * * work with all elements of a specific function array in group * @param string & $array strings to be processed * @param string $function functions to perform * @return boolean $apply _ to_keys_also is also applied to key * @access public * *************************************************************/ 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--; } /******************************************** * * converting arrays to JSON strings (compatible with Chinese) * @param array $array Array to convert * @ return string conversion of JSON string * @access p ublic * *************************************************************/ Function json ($array) { arrayrecursive ($array, ' UrlEncode ', true); $json = json_encode ($array); return urldecode ($json); } $ array = array ( ' Name ' => ' Shia ', ' age ' =>20 );
/* $array =array ( 0 => array ( ' icon ' => array (&nbsP ' Hasphoto ' => ' 0 ', ' Photopath ' => '/resources/v20/images/boy.png ', ), ' age ' => ', ' name ' => ' Men ', ' province ' => ' Beijing ', ' lottery ' => ' 100 Yuan of Emma EV voucher ', ' mobile ' => ', ), 1 => array ( ' icon ' => array ( ' Hasphoto ' => ' 0 ', ' photopath ' => '/resources/v20/images/boy.png ', , ' age ' => ', ' name ' => ' man ', ' province ' => ' Beijing ' , ' lottery ' => ' 100 Yuan of Emma's EV voucher ', ' Mobile ' => ', , 2 => array
( ' icon ' => array ( ) ' Hasphoto ' => ' 0 ', ' Photopath ' => '/resources/v20/images/boy.png ', ), ' age ' => ', ' name ' => ' Men ', ' province ' => ' Shanghai ', ' lottery ' => ' 100 Yuan of Emma EV voucher ', ' mobile ' => ', ), &Nbsp; 3 => array ( ' icon ' => array ( ' Hasphoto ' = > ' 0 ', ' photopath ' => '/resources/v20/images/ Boy.png ', , ' age ' => ', ' name ' => ' man ', ' province ' => ' Beijing ', ' lottery ' => ' 100 Yuan of Emma's EV voucher ', ' mobile ' => ' 186****1046 ', ), 4 => array
( ' icon ' => array ( ) ' Hasphoto ' => ' 0 ', ' Photopath ' => '/resources/v20/images/boy.png ', ), ' age ' => ', ' name ' => ' Men ', ' province ' => ' Beijing ', ' lottery ' = > ' 200 Yuan of Emma's EV voucher ', ' mobile ' => ' 186****1046 ', ), 5 => array ( ' icon ') => array ( ' Hasphoto ' => ' 0 ', ' photopath ' => '/ Resources/v20/images/boy.png ', , ' age ' => ', ' name ' => ' man ', ' province ' & nbsp;=> ' Beijing ', ' lottery ' => ' 100 Yuan of Emma's EV voucher ', ' mobile ' => ', , 6 => array ( ' icon ' => array ' hasphoto ' => ' 0 ',
' Photopath ' => '/resources/v20/images/boy.png ', ), ' age ' => ', ' name ' => ' Men ', ' province ' => ' Beijing ', ' lottery ' =
> ' 100 $ Emma's EV voucher ', ' mobile ' => ', ), 7 => array ( ' ICon ' => array ( ' Hasphoto ' => ' 0 ', ' photopath ' => '/ Resources/v20/images/boy.png ', , ' age ' => ', ' name ' => ' man ', ' province ' => ' Beijing ', ' lottery ' => ' 100 Yuan of Emma's EV voucher ',
' mobile ' => ', , 8 => array ( ' icon ' => array ( ' hasphoto ' => ' 0 ', ' Photopath ' => '/resources/v20/images/boy.png ', ),   ' age ' => ', ' name ' => ' Men ', ' province ' => ' Henan ', ' lottery ' =
> ' 100 $ Emma's EV voucher ', ' mobile ' => ', ), 9 => array ( ' icon ' => array ( ' Hasphoto ' = > ' 0 ', ' photopath ' => '/resources/v20/images/ Boy.png ', , ' age ' => ', ' name ' => ' man ', ' province ' => ' Beijing ', ' lottery ' => ' 100 Yuan of Emma EV voucher ', &NBSP;&NBSP;&NBsp; ' mobile ' => ', ), 10 => array ( ' icon ' => array ' Hasphoto ' => ' 1 ', ' Photopath ' => '/201412/11/11/49/1418269782350a03ea57_c.jpg ', ), ' age ' => ', ' name ' = > ' daydreaming ', ' province ' => ' Beijing ', '
Lottery ' => ' 100 Yuan of Emma's EV voucher ', ' mobile ' => ', ), ); */ Echo json ($array); &
nbsp ?>
The JSON object is turned into
The simplest way to use an array, or the Json_decode () method, is to write one more argument in the back to get it done.
Json_decode ($json, true);
This allows you to convert JSON to an array of forms, and the key stays in the original format
$json = ' {' name ': ' Zhangsan ', ' age ': ' Sex ': ' nan '} ';
Print_r (Json_decode ($json, true));
Such JSON data will be parsed to be the following array
Array
(
[Name] => Zhangsan
[Age] => 20
[Sex] => Nan
)
Array to JSON Chinese character
<?php $josin =array ( ' 0 ' =>array ( ' name ' => ' One World ', ' subname ' => ' Nanyang Store ', ' AGV ' > ' 5 ', ' Add ' => ' Metro station E24 ', ' Tel ' => ' 13382041088 ', ), ' 1 ' =>array ( ' name ' => ' Sihai two ', ' subname ' => ' Nanyang Store ',
' AGV ' => ' 5 ', ' Add ' => ' subway station E24 ', ' Tel ' => ' 13382041088 ', ), ' 2 ' =>array ( ' name ' => ' Sihai three ', ' subname ' => ' Nanyang Store ', ' AGV ' => ' 5 ', ' Add ' => ' Metro station E24 ', ' tel ' => ' 13382041088 ', ), ' 3 ' =>array ( ' name ' => ') Sihai four ', ' subname ' => ' Nanyang Store ', ' AGV ' => ' 5 ', ' Add ' => ' Metro station E24 ', ' Tel ' => ' 13382041088 ', ), ' 4 ' =>array ( ' name ' => ' Sihai five ', ' subname ' => ' Nanyang Shop ', ' AGV ' => ' 5 ', ' Add ' => ' Metro station E24 ', ' Tel ' => ' 13382041088 ', ),
);
$k =json ($josin);
echo $k; /******************************************** * * Handle all elements in the array with a specific function * @param string &$ array the string to be processed * @param string $function functions to perform * @return boolean $apply _to_keys_also is also applied to key * @ Access public * ******************************* / 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); } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; 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 Array to JSON string (compatible Chinese) * @param array $array The array to convert * @return string
The converted JSON string * @access public * *************************************************************/ function json ($array) { arrayrecursive ($array,
' UrlEncode ', true);
$json = json_encode ($array);
return urldecode ($json); }