PHP数组对象与Json转换操作实例分析

这篇文章主要介绍了PHP数组对象与Json转换操作,结合实例形式分析了php数组对象与json相互转换的实现方法及相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP数组对象与Json转换操作,分享给大家供大家参考,具体如下:

代码

  1. <?php
  2. //数组转对象
  3. function arrayToObject($e){
  4. if( gettype($e)!='array' ) return;
  5. foreach($e as $k=>$v){
  6. if( gettype($v)=='array' || getType($v)=='object' )
  7. $e[$k]=(object)arrayToObject($v);
  8. }
  9. return (object)$e;
  10. }
  11. //对象转数组
  12. function objectToArray($e){
  13. $e=(array)$e;
  14. foreach($e as $k=>$v){
  15. if( gettype($v)=='resource' ) return;
  16. if( gettype($v)=='object' || gettype($v)=='array' )
  17. $e[$k]=(array)objectToArray($v);
  18. }
  19. return $e;
  20. }
  21. //对象转数组
  22. function object_array($array){
  23. if(is_object($array)){
  24. $array = (array)$array;
  25. }
  26. if(is_array($array)){
  27. foreach($array as $key=>$value){
  28. $array[$key] = object_array($value);
  29. }
  30. }
  31. return $array;
  32. }
  33. //json到数组
  34. function simple_json_parser($json){
  35. $json = str_replace("{","",str_replace("}","", $json));
  36. $jsonValue = explode(",", $json);
  37. $arr = array();
  38. foreach($jsonValue as $v){
  39. $jValue = explode(":", $v);
  40. $arr[str_replace('"',"", $jValue[0])] = (str_replace('"', "", $jValue[1]));
  41. }
  42. return $arr;
  43. }
  44. echo "<br>-------------------------------<br>";
  45. echo "现在的数组<br><br>";
  46. $arr=array(
  47. "name"=>"李小龙",
  48. "tel"=>"11111111111",
  49. "age"=>"12",
  50. );
  51. var_dump($arr);
  52. echo "<br>-------------------------------<br>";
  53. echo "<br><br>数组转对象<br><br>";
  54. $obj= arrayToObject($arr);
  55. var_dump($obj);
  56. echo "<br><br>输出对象属性<br><br>";
  57. var_dump($obj->name);
  58. echo "<br>-------------------------------<br>";
  59. echo "<br><br>对象转数组<br><br>";
  60. $arr=object_array($obj);
  61. var_dump($arr);
  62. echo "<br><br>输出数组数值<br><br>";
  63. var_dump($arr["name"]);
  64. echo "<br>-------------------------------<br>";
  65. echo "<br><br>Json到数组<br><br>";
  66. $json ='{"a":"哈哈","b":"我","c":"数","d":"一","e":3,"f":2,"g":1,"h":"别生气了"}';
  67. $arr=simple_json_parser($json);
  68. var_dump($arr);
  69. echo "<br>官方换种方法<br>";
  70. echo "对象<br>";
  71. var_dump(json_decode($json));
  72. $a=json_decode($json);
  73. echo "<br>属性:";
  74. echo $a->a;
  75. echo "<br>数组<br>";
  76. var_dump(json_decode($json,true));
  77. echo "<br>-------------------------------<br>";
  78. echo "<br><br>数组到Json<br><br>";
  79. var_dump($arr);
  80. echo "<br>";
  81. var_dump(json_encode($json, JSON_HEX_APOS));

输出

------------------------------

现在的数组

  1. array(3) { ["name"]=> string(9) "李小龙" ["tel"]=> string(11) "11111111111" ["age"]=> string(2) "12" }
  2. -------------------------------

数组转对象

object(stdClass)#1 (3) { ["name"]=> string(9) "李小龙" ["tel"]=> string(11) "11111111111" ["age"]=> string(2) "12" }

输出对象属性

string(9) "李小龙"

对象转数组

array(3) { ["name"]=> string(9) "李小龙" ["tel"]=> string(11) "11111111111" ["age"]=> string(2) "12" }

输出数组数值

string(9) "李小龙"

Json到数组

array(8) { ["a"]=> string(6) "哈哈" ["b"]=> string(3) "我" ["c"]=> string(3) "数" ["d"]=> string(3) "一" ["e"]=> string(1) "3" ["f"]=> string(1) "2" ["g"]=> string(1) "1" ["h"]=> string(12) "别生气了" }

官方换种方法

对象

object(stdClass)#2 (8) { ["a"]=> string(6) "哈哈" ["b"]=> string(3) "我" ["c"]=> string(3) "数" ["d"]=> string(3) "一" ["e"]=> int(3) ["f"]=> int(2) ["g"]=> int(1) ["h"]=> string(12) "别生气了" }

属性:哈哈

数组

array(8) { ["a"]=> string(6) "哈哈" ["b"]=> string(3) "我" ["c"]=> string(3) "数" ["d"]=> string(3) "一" ["e"]=> int(3) ["f"]=> int(2) ["g"]=> int(1) ["h"]=> string(12) "别生气了" }

数组到Json

array(8) { ["a"]=> string(6) "哈哈" ["b"]=> string(3) "我" ["c"]=> string(3) "数" ["d"]=> string(3) "一" ["e"]=> string(1) "3" ["f"]=> string(1) "2" ["g"]=> string(1) "1" ["h"]=> string(12) "别生气了" }

string(136) ""{\"a\":\"\u54c8\u54c8\",\"b\":\"\u6211\",\"c\":\"\u6570\",\"d\":\"\u4e00\",\"e\":3,\"f\":2,\"g\":1,\"h\":\"\u522b\u751f\u6c14\u4e86\"}""