国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP中數(shù)組轉(zhuǎn)換成json字符串程序代碼

2024-09-04 11:46:27
字體:
供稿:網(wǎng)友

數(shù)據(jù)轉(zhuǎn)換js格式的數(shù)據(jù)是我們常用一種數(shù)據(jù)傳遞的方法,特別像ajax中會時常用到把數(shù)據(jù)轉(zhuǎn)換成json然后再轉(zhuǎn)換回來,下面看一個實例,代碼如下:

  1. function array_to_json($array) { 
  2.  if (! is_array ( $array )) { 
  3.   return false; 
  4.  } 
  5.  
  6.  $associative = count ( array_diff ( array_keys ( $array ), array_keys ( array_keys ( $array ) ) ) ); 
  7.  if ($associative) { 
  8.    
  9.   $construct = array (); 
  10.   foreach ( $array as $key => $value ) { 
  11.     
  12.    // We first copy each key/value pair into a staging array, 
  13.    // formatting each key and value properly as we go. 
  14.    
  15.  
  16.    // Format the key: 
  17.    if (is_numeric ( $key )) { 
  18.     $key = "key_$key"
  19.    } 
  20.    $key = """ . addslashes ( $key ) . """
  21.     
  22.    // Format the value: 
  23.    if (is_array ( $value )) { 
  24.     $value = array_to_json ( $value ); 
  25.    } else if (! is_numeric ( $value ) || is_string ( $value )) { 
  26.     $value = """ . addslashes ( $value ) . """
  27.    } 
  28.     
  29.    // Add to staging array: 
  30.    $construct [] = "$key: $value"
  31.   } 
  32.    
  33.   // Then we collapse the staging array into the JSON form: 
  34.   $result = "{" . implode ( ","$construct ) . "}"
  35.  
  36.  } else { // If the array is a vector (not associative): 
  37.  
  38.  
  39.   $construct = array (); 
  40.   foreach ( $array as $value ) { 
  41.     
  42.    // Format the value: 
  43.    if (is_array ( $value )) { 
  44.     $value = array_to_json ( $value ); 
  45.    } else if (! is_numeric ( $value ) || is_string ( $value )) { 
  46.     $value = """ . addslashes ( $value ) . """
  47.    }//開源軟件:Vevb.com 
  48.     
  49.    // Add to staging array: 
  50.    $construct [] = $value
  51.   } 
  52.    
  53.   // Then we collapse the staging array into the JSON form: 
  54.   $result = "[" . implode ( ", "$construct ) . "]"
  55.  } 
  56.  
  57.  return $result

你可以試試這個,然后json_encode換成上面的函數(shù)看看正常了嗎,代碼如下:

  1. <?php 
  2. if($_GET['enews']=='ok'){ 
  3.     echo json_encode(array('a'=>'王進(jìn)'));exit
  4. ?> 
  5. <script type="text/javascript" src="jquery.js"></script> 
  6. <script type="text/javascript"
  7. $(function(){ 
  8.     $.get("?enews=ok"function(result){ 
  9.     alert(result); 
  10.  }); 
  11. }); 
  12. </script> 

關(guān)于php中json_encode

json_encode()將PHP的不同類型的變量轉(zhuǎn)換為對應(yīng)的JSON字符串 string json_encode(mixed $value [,int $options = 0])

PHP 5.3.0

JSON_HEX_QUOT:將所有的雙引號(”)轉(zhuǎn)換為u0022,實例代碼如下:

  1. $data = '"'
  2. echo json_encode($data); // """ 
  3. echo json_encode($data, JSON_HEX_QUOT); 
  4.  // "u0022" 

JSON_HEX_TAG:將所有的大于號(>)轉(zhuǎn)換為u003E,將所有的小于號(<)轉(zhuǎn)換為 u003C。

JSON_HEX_AMP:將所有的與號(&)轉(zhuǎn)換為 u0026。

JSON_HEX_APOS:將所有的單引號(’)轉(zhuǎn)換為u0027。

JSON_FORCE_OBJECT:當(dāng)value為非關(guān)聯(lián)數(shù)組時強(qiáng)制輸出結(jié)果為JSON對象。在接收者要求數(shù)據(jù)為對象且value為空數(shù)組時

使用,實例代碼如下:

  1. $data = array(); 
  2. echo json_encode($data); // [] 
  3. echo json_encode($data, JSON_FORCE_OBJECT); // {} 
  4. PHP 5.3.3 
  5. JSON_NUMERIC_CHECK: Encodes numeric strings as numbers. 
  6. PHP 5.4.0 
  7. JSON_BIGINT_AS_STRING: Encodes large integers as their original string value. Available since PHP  
  8. 5.4.0.//開源軟件:Vevb.com 
  9. JSON_PRETTY_PRINT: Use whitespace in returned data to format it. Available since PHP 5.4.0. 
  10. JSON_UNESCAPED_SLASHES: Don’t escape /. Available since PHP 5.4.0. 
  11. JSON_UNESCAPED_UNICODE: Encode multibyte Unicode characters literally (default is to escape as uXXXX). 
  12. Available since PHP 5.4.0.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 若羌县| 连南| 曲松县| 新昌县| 炉霍县| 高阳县| 南召县| 民权县| 新平| 固阳县| 文山县| 桦川县| 桃江县| 清徐县| 遂平县| 青龙| 永丰县| 博野县| 阿拉善右旗| 阿城市| 襄城县| 遵义市| 府谷县| 武安市| 赤城县| 久治县| 八宿县| 巴塘县| 盱眙县| 曲阳县| 新安县| 高唐县| 沿河| 广宗县| 通河县| 长乐市| 光山县| 沁阳市| 临城县| 惠水县| 互助|