php数组转Json的例子

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

数组转Json,代码如下:

  1. <?php
  2. header("Content-Type: text/html; charset=utf-8");
  3. $mydb=mysql_connect("localhost","root","root");
  4. if (!$mydb){
  5. die('Could not connect:'. mysql_error());
  6. }
  7. $db_<a href="/tags.php/select/" target="_blank">select</a>ed=mysql_select_db("mysql",$mydb);
  8. //$sql = "SELECT * from Person WHERE Lastname='Adams'";
  9. $sql="SELECT * from user";
  10. $result=mysql_query($sql,$mydb);
  11. //print_r(mysql_fetch_array($result));
  12. //处理输出数组格式
  13. //$db1=mysql_query("select * from `tb_info`");
  14. /*
  15. $arr=array();
  16. while($rows=mysql_fetch_array($db1)){
  17. $key=$rows['id']
  18. $arr[$key] = $rows['qucount']
  19. }
  20. */
  21. /**************************************************************
  22. *
  23. * 使用特定function对数组中所有元素做处理
  24. * @param string &$array 要处理的字符串
  25. * @param string $function 要执行的函数
  26. * @return boolean $apply_to_keys_also 是否也应用到key上
  27. * @access public
  28. *
  29. *************************************************************/
  30. function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
  31. {
  32. static $recursive_counter = 0;
  33. if (++$recursive_counter > 1000) {
  34. die('possible deep recursion attack');
  35. }
  36. <a href="/tags.php/foreach/" target="_blank">foreach</a> ($array as $key => $value) {
  37. if (is_array($value)) {
  38. arrayRecursive($array[$key], $function, $apply_to_keys_also);
  39. } else {
  40. $array[$key] = $function($value);
  41. }
  42. if ($apply_to_keys_also && is_string($key)) {
  43. $new_key = $function($key);
  44. if ($new_key != $key) {
  45. $array[$new_key] = $array[$key];
  46. unset($array[$key]);
  47. }
  48. }
  49. }
  50. $recursive_counter--;
  51. }
  52. /**************************************************************
  53. *
  54. * 将数组转换为JSON字符串(兼容中文)
  55. * @param array $array 要转换的数组
  56. * @return string 转换得到的json字符串
  57. * @access public
  58. *
  59. *************************************************************/
  60. function JSON($array) {
  61. arrayRecursive($array, 'urlencode', true);
  62. $json = json_encode($array);
  63. return urldecode($json);
  64. }
  65. $array = array
  66. (
  67. 'Name'=>'希亚',
  68. 'Age'=>20
  69. );
  70. /*
  71. $array=array (
  72. 0 =>
  73. array (
  74. 'icon' =>
  75. array (
  76. 'hasPhoto' => '0',
  77. 'photoPath' => '/resources/v20/images/boy.png',
  78. ),
  79. 'age' => '24',
  80. 'name' => '男士',
  81. 'province' => '北京',
  82. 'lottery' => '100元的爱玛电动车代金券',
  83. 'mobile' => '',
  84. ),
  85. 1 =>
  86. array (
  87. 'icon' =>
  88. array (
  89. 'hasPhoto' => '0',
  90. 'photoPath' => '/resources/v20/images/boy.png',
  91. ),
  92. 'age' => '24',
  93. 'name' => '男士',
  94. 'province' => '北京',
  95. 'lottery' => '100元的爱玛电动车代金券',
  96. 'mobile' => '',
  97. ),
  98. 2 =>
  99. array (
  100. 'icon' =>
  101. array (
  102. 'hasPhoto' => '0',
  103. 'photoPath' => '/resources/v20/images/boy.png',
  104. ),
  105. 'age' => '25',
  106. 'name' => '男士',
  107. 'province' => '上海',
  108. 'lottery' => '100元的爱玛电动车代金券',
  109. 'mobile' => '',
  110. ),
  111. 3 =>
  112. array (
  113. 'icon' =>
  114. array (
  115. 'hasPhoto' => '0',
  116. 'photoPath' => '/resources/v20/images/boy.png',
  117. ),
  118. 'age' => '24',
  119. 'name' => '男士',
  120. 'province' => '北京',
  121. 'lottery' => '100元的爱玛电动车代金券',
  122. 'mobile' => '186****1046',
  123. ),
  124. 4 =>
  125. array (
  126. 'icon' =>
  127. array (
  128. 'hasPhoto' => '0',
  129. 'photoPath' => '/resources/v20/images/boy.png',
  130. ),
  131. 'age' => '24',
  132. 'name' => '男士',
  133. 'province' => '北京',
  134. 'lottery' => '200元的爱玛电动车代金券',
  135. 'mobile' => '186****1046',
  136. ),
  137. 5 =>
  138. array (
  139. 'icon' =>
  140. array (
  141. 'hasPhoto' => '0',
  142. 'photoPath' => '/resources/v20/images/boy.png',
  143. ),
  144. 'age' => '24',
  145. 'name' => '男士',
  146. 'province' => '北京',
  147. 'lottery' => '100元的爱玛电动车代金券',
  148. 'mobile' => '',
  149. ),
  150. 6 =>
  151. array (
  152. 'icon' =>
  153. array (
  154. 'hasPhoto' => '0',
  155. 'photoPath' => '/resources/v20/images/boy.png',
  156. ),
  157. 'age' => '24',
  158. 'name' => '男士',
  159. 'province' => '北京',
  160. 'lottery' => '100元的爱玛电动车代金券',
  161. 'mobile' => '',
  162. ),
  163. 7 =>
  164. array (
  165. 'icon' =>
  166. array (
  167. 'hasPhoto' => '0',
  168. 'photoPath' => '/resources/v20/images/boy.png',
  169. ),
  170. 'age' => '24',
  171. 'name' => '男士',
  172. 'province' => '北京',
  173. 'lottery' => '100元的爱玛电动车代金券',
  174. 'mobile' => '',
  175. ),
  176. 8 =>
  177. array (
  178. 'icon' =>
  179. array (
  180. 'hasPhoto' => '0',
  181. 'photoPath' => '/resources/v20/images/boy.png',
  182. ),
  183. 'age' => '24',
  184. 'name' => '男士',
  185. 'province' => '河南',
  186. 'lottery' => '100元的爱玛电动车代金券',
  187. 'mobile' => '',
  188. ),
  189. 9 =>
  190. array (
  191. 'icon' =>
  192. array (
  193. 'hasPhoto' => '0',
  194. 'photoPath' => '/resources/v20/images/boy.png',
  195. ),
  196. 'age' => '24',
  197. 'name' => '男士',
  198. 'province' => '北京',
  199. 'lottery' => '100元的爱玛电动车代金券',
  200. 'mobile' => '',
  201. ),
  202. 10 =>
  203. array (
  204. 'icon' =>
  205. array (
  206. 'hasPhoto' => '1',
  207. 'photoPath' => '/201412/11/11/49/1418269782350A03EA57_c.jpg',
  208. ),
  209. 'age' => '20',
  210. 'name' => '白日做梦',
  211. 'province' => '北京',
  212. 'lottery' => '100元的爱玛电动车代金券',
  213. 'mobile' => '',
  214. ),
  215. );
  216. */
  217. echo JSON($array);
  218. ?>

json对象转成,代码如下:

普通数组也就是 Array 的最简单方法还是用 json_decode() 方法,只需要在后面多写一个参数就可以搞定.

json_decode($json,true);

这样就可以将 json 转换成数组形式了,key 保持原来格式.

$json = ’{“name”:”zhangsan”,”age”:20,”sex”:”nan”}’;

print_r(json_decode($json,true));

这样的json数据解析后就会成为下面这样的数组.

  1. Array
  2. (
  3. [name] => zhangsan
  4. [age] => 20
  5. [sex] => nan
  6. )

数组转json 中文字符,代码如下:

  1. <?php
  2. $josin=array(
  3. '0'=>array(
  4. 'name'=>'四海一家',
  5. 'subname'=>'南阳店',
  6. 'agv'=>'5',
  7. 'add'=>'新街口地铁站E24号',
  8. 'tel'=>'13382041088',
  9. ), //开源软件:phpfensi.com
  10. '1'=>array(
  11. 'name'=>'四海二家',
  12. 'subname'=>'南阳店',
  13. 'agv'=>'5',
  14. 'add'=>'新街口地铁站E24号',
  15. 'tel'=>'13382041088',
  16. ),
  17. '2'=>array(
  18. 'name'=>'四海三家',
  19. 'subname'=>'南阳店',
  20. 'agv'=>'5',
  21. 'add'=>'新街口地铁站E24号',
  22. 'tel'=>'13382041088',
  23. ),
  24. '3'=>array(
  25. 'name'=>'四海四家',
  26. 'subname'=>'南阳店',
  27. 'agv'=>'5',
  28. 'add'=>'新街口地铁站E24号',
  29. 'tel'=>'13382041088',
  30. ),
  31. '4'=>array(
  32. 'name'=>'四海五家',
  33. 'subname'=>'南阳店',
  34. 'agv'=>'5',
  35. 'add'=>'新街口地铁站E24号',
  36. 'tel'=>'13382041088',
  37. ),
  38. );
  39. $k=JSON($josin);
  40. echo $k;
  41. /**************************************************************
  42. *
  43. * 使用特定function对数组中所有元素做处理
  44. * @param string &$array 要处理的字符串
  45. * @param string $function 要执行的函数
  46. * @return boolean $apply_to_keys_also 是否也应用到key上
  47. * @access public
  48. *
  49. *************************************************************/
  50. function arrayRecursive(&$array, $function, $apply_to_keys_also = false)
  51. {
  52. static $recursive_counter = 0;
  53. if (++$recursive_counter > 1000) {
  54. die('possible deep recursion attack');
  55. }
  56. foreach ($array as $key => $value) {
  57. if (is_array($value)) {
  58. arrayRecursive($array[$key], $function, $apply_to_keys_also);
  59. } else {
  60. $array[$key] = $function($value);
  61. }
  62. if ($apply_to_keys_also && is_string($key)) {
  63. $new_key = $function($key);
  64. if ($new_key != $key) {
  65. $array[$new_key] = $array[$key];
  66. unset($array[$key]);
  67. }
  68. }
  69. }
  70. $recursive_counter--;
  71. }
  72. /**************************************************************
  73. *
  74. * 将数组转换为JSON字符串(兼容中文)
  75. * @param array $array 要转换的数组
  76. * @return string 转换得到的json字符串
  77. * @access public
  78. *
  79. *************************************************************/
  80. function JSON($array) {
  81. arrayRecursive($array, 'urlencode', true);
  82. $json = json_encode($array);
  83. return urldecode($json);
  84. }
  85. ?>