ホームページ  >  記事  >  php教程  >  php数组转Json的例子

php数组转Json的例子

WBOY
WBOYオリジナル
2016-06-08 17:20:311759ブラウズ

数组与Json格式其实是非常像了,我们可以利用相关的函数来进行相互转换的,下面来给各位整理一个从mysql读取数据之后再利用php函数转换成json回传,具体如下

<script>ec(2);</script>

数组转Json

<?php  

header("Content-Type: text/html; charset=utf-8");  

  

$mydb=mysql_connect("localhost","root","root");  

if (!$mydb){  

  die(&#39;Could not connect:&#39;. mysql_error());  

}  

  

$db_selected=mysql_select_db("mysql",$mydb);  

//$sql = "SELECT * from Person WHERE Lastname=&#39;Adams&#39;";  

$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[&#39;id&#39;] 

$arr[$key] = $rows[&#39;qucount&#39;] 

} 

*/  

  

/************************************************************** 

 * 

 *  使用特定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(&#39;possible deep recursion attack&#39;);  

    }  

    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, &#39;urlencode&#39;, true);  

    $json = json_encode($array);  

    return urldecode($json);  

}  

  

   

  

$array = array  

       (  

          &#39;Name&#39;=>&#39;希亚&#39;,  

          &#39;Age&#39;=>20  

       );  

  

/* 

$array=array ( 

  0 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

  1 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

  2 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;25&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;上海&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

  3 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;186****1046&#39;, 

  ), 

  4 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;200元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;186****1046&#39;, 

  ), 

  5 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

  6 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

  7 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

  8 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;河南&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

  9 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;0&#39;, 

      &#39;photoPath&#39; => &#39;/resources/v20/images/boy.png&#39;, 

    ), 

    &#39;age&#39; => &#39;24&#39;, 

    &#39;name&#39; => &#39;男士&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

  10 =>  

  array ( 

    &#39;icon&#39; =>  

    array ( 

      &#39;hasPhoto&#39; => &#39;1&#39;, 

      &#39;photoPath&#39; => &#39;/201412/11/11/49/1418269782350A03EA57_c.jpg&#39;, 

    ), 

    &#39;age&#39; => &#39;20&#39;, 

    &#39;name&#39; => &#39;白日做梦&#39;, 

    &#39;province&#39; => &#39;北京&#39;, 

    &#39;lottery&#39; => &#39;100元的爱玛电动车代金券&#39;, 

    &#39;mobile&#39; => &#39;&#39;, 

  ), 

); 

*/  

  

    

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(
     &#39;0&#39;=>array(
            &#39;name&#39;=>&#39;四海一家&#39;,
            &#39;subname&#39;=>&#39;南阳店&#39;,
            &#39;agv&#39;=>&#39;5&#39;,
            &#39;add&#39;=>&#39;新街口地铁站E24号&#39;,
            &#39;tel&#39;=>&#39;13382041088&#39;,
      ),
      &#39;1&#39;=>array(
            &#39;name&#39;=>&#39;四海二家&#39;,
            &#39;subname&#39;=>&#39;南阳店&#39;,
            &#39;agv&#39;=>&#39;5&#39;,
            &#39;add&#39;=>&#39;新街口地铁站E24号&#39;,
            &#39;tel&#39;=>&#39;13382041088&#39;,
      ),
           &#39;2&#39;=>array(
            &#39;name&#39;=>&#39;四海三家&#39;,
            &#39;subname&#39;=>&#39;南阳店&#39;,
            &#39;agv&#39;=>&#39;5&#39;,
            &#39;add&#39;=>&#39;新街口地铁站E24号&#39;,
            &#39;tel&#39;=>&#39;13382041088&#39;,
      ),
           &#39;3&#39;=>array(
            &#39;name&#39;=>&#39;四海四家&#39;,
            &#39;subname&#39;=>&#39;南阳店&#39;,
            &#39;agv&#39;=>&#39;5&#39;,
            &#39;add&#39;=>&#39;新街口地铁站E24号&#39;,
            &#39;tel&#39;=>&#39;13382041088&#39;,
      ),
           &#39;4&#39;=>array(
            &#39;name&#39;=>&#39;四海五家&#39;,
            &#39;subname&#39;=>&#39;南阳店&#39;,
            &#39;agv&#39;=>&#39;5&#39;,
            &#39;add&#39;=>&#39;新街口地铁站E24号&#39;,
            &#39;tel&#39;=>&#39;13382041088&#39;,
      ),
  
);
$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(&#39;possible deep recursion attack&#39;);
        }
        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, &#39;urlencode&#39;, true);
        $json = json_encode($array);
        return urldecode($json);
    }

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。