php数组转Json的例子
数组与Json格式其实是非常像了,我们可以利用相关的函数来进行相互转换的,下面来给各位整理一个从mysql读取数据之后再利用php函数转换成json回传,具体如下.
数组转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_<a href="/tags.php/select/" target="_blank">select</a>ed=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));
- //处理输出数组格式
- //$db1=mysql_query("select * from `tb_info`");
- /*
- $arr=array();
- while($rows=mysql_fetch_array($db1)){
- $key=$rows['id']
- $arr[$key] = $rows['qucount']
- }
- */
- /**************************************************************
- *
- * 使用特定function对数组中所有元素做处理
- * @param string &$array 要处理的字符串
- * @param string $function 要执行的函数
- * @return boolean $apply_to_keys_also 是否也应用到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');
- }
- <a href="/tags.php/foreach/" target="_blank">foreach</a> ($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--;
- }
- /**************************************************************
- *
- * 将数组转换为JSON字符串(兼容中文)
- * @param array $array 要转换的数组
- * @return string 转换得到的json字符串
- * @access public
- *
- *************************************************************/
- function JSON($array) {
- arrayRecursive($array, 'urlencode', true);
- $json = json_encode($array);
- return urldecode($json);
- }
- $array = array
- (
- 'Name'=>'希亚',
- 'Age'=>20
- );
- /*
- $array=array (
- 0 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 1 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 2 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '25',
- 'name' => '男士',
- 'province' => '上海',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 3 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '186****1046',
- ),
- 4 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '200元的爱玛电动车代金券',
- 'mobile' => '186****1046',
- ),
- 5 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 6 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 7 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 8 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '河南',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 9 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '0',
- 'photoPath' => '/resources/v20/images/boy.png',
- ),
- 'age' => '24',
- 'name' => '男士',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- 10 =>
- array (
- 'icon' =>
- array (
- 'hasPhoto' => '1',
- 'photoPath' => '/201412/11/11/49/1418269782350A03EA57_c.jpg',
- ),
- 'age' => '20',
- 'name' => '白日做梦',
- 'province' => '北京',
- 'lottery' => '100元的爱玛电动车代金券',
- 'mobile' => '',
- ),
- );
- */
- echo JSON($array);
- ?>
json对象转成,代码如下:
普通数组也就是 Array 的最简单方法还是用 json_decode() 方法,只需要在后面多写一个参数就可以搞定.
json_decode($json,true);
这样就可以将 json 转换成数组形式了,key 保持原来格式.
$json = ’{“name”:”zhangsan”,”age”:20,”sex”:”nan”}’;
print_r(json_decode($json,true));
这样的json数据解析后就会成为下面这样的数组.
- Array
- (
- [name] => zhangsan
- [age] => 20
- [sex] => nan
- )
数组转json 中文字符,代码如下:
- <?php
- $josin=array(
- '0'=>array(
- 'name'=>'四海一家',
- 'subname'=>'南阳店',
- 'agv'=>'5',
- 'add'=>'新街口地铁站E24号',
- 'tel'=>'13382041088',
- ), //开源软件:phpfensi.com
- '1'=>array(
- 'name'=>'四海二家',
- 'subname'=>'南阳店',
- 'agv'=>'5',
- 'add'=>'新街口地铁站E24号',
- 'tel'=>'13382041088',
- ),
- '2'=>array(
- 'name'=>'四海三家',
- 'subname'=>'南阳店',
- 'agv'=>'5',
- 'add'=>'新街口地铁站E24号',
- 'tel'=>'13382041088',
- ),
- '3'=>array(
- 'name'=>'四海四家',
- 'subname'=>'南阳店',
- 'agv'=>'5',
- 'add'=>'新街口地铁站E24号',
- 'tel'=>'13382041088',
- ),
- '4'=>array(
- 'name'=>'四海五家',
- 'subname'=>'南阳店',
- 'agv'=>'5',
- 'add'=>'新街口地铁站E24号',
- 'tel'=>'13382041088',
- ),
- );
- $k=JSON($josin);
- echo $k;
- /**************************************************************
- *
- * 使用特定function对数组中所有元素做处理
- * @param string &$array 要处理的字符串
- * @param string $function 要执行的函数
- * @return boolean $apply_to_keys_also 是否也应用到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--;
- }
- /**************************************************************
- *
- * 将数组转换为JSON字符串(兼容中文)
- * @param array $array 要转换的数组
- * @return string 转换得到的json字符串
- * @access public
- *
- *************************************************************/
- function JSON($array) {
- arrayRecursive($array, 'urlencode', true);
- $json = json_encode($array);
- return urldecode($json);
- }
- ?>