PHP中数组转换成json字符串程序代码

数据转换js格式的数据是我们常用一种数据传递的方法,特别像ajax中会时常用到把数据转换成json然后再转换回来,下面看一个实例,代码如下:

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

你可以试试这个,然后json_encode换成上面的函数看看正常了吗,代码如下:

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

关于php中json_encode

json_encode()将PHP的不同类型的变量转换为对应的JSON字符串 string json_encode(mixed $value [,int $options = 0])

PHP 5.3.0

JSON_HEX_QUOT:将所有的双引号(”)转换为u0022,实例代码如下:

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

JSON_HEX_TAG:将所有的大于号(>)转换为u003E,将所有的小于号(<)转换为 u003C。

JSON_HEX_AMP:将所有的与号(&)转换为 u0026。

JSON_HEX_APOS:将所有的单引号(’)转换为u0027。

JSON_FORCE_OBJECT:当value为非关联数组时强制输出结果为JSON对象。在接收者要求数据为对象且value为空数组时

使用,实例代码如下:

  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.//开源软件:phpfensi.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.