PHP数组对象与Json转换操作实例分析
本文实例讲述了php数组对象与json转换操作。分享给大家供大家参考,具体如下:
代码
<?php //数组转对象 function arraytoobject($e){ if( gettype($e)!='array' ) return; foreach($e as $k=>$v){ if( gettype($v)=='array' || gettype($v)=='object' ) $e[$k]=(object)arraytoobject($v); } return (object)$e; } //对象转数组 function objecttoarray($e){ $e=(array)$e; foreach($e as $k=>$v){ if( gettype($v)=='resource' ) return; if( gettype($v)=='object' || gettype($v)=='array' ) $e[$k]=(array)objecttoarray($v); } return $e; } //对象转数组 function object_array($array){ if(is_object($array)){ $array = (array)$array; } if(is_array($array)){ foreach($array as $key=>$value){ $array[$key] = object_array($value); } } return $array; } //json到数组 function simple_json_parser($json){ $json = str_replace("{","",str_replace("}","", $json)); $jsonvalue = explode(",", $json); $arr = array(); foreach($jsonvalue as $v){ $jvalue = explode(":", $v); $arr[str_replace('"',"", $jvalue[0])] = (str_replace('"', "", $jvalue[1])); } return $arr; } echo "<br>-------------------------------<br>"; echo "现在的数组<br><br>"; $arr=array( "name"=>"李小龙", "tel"=>"11111111111", "age"=>"12", ); var_dump($arr); echo "<br>-------------------------------<br>"; echo "<br><br>数组转对象<br><br>"; $obj= arraytoobject($arr); var_dump($obj); echo "<br><br>输出对象属性<br><br>"; var_dump($obj->name); echo "<br>-------------------------------<br>"; echo "<br><br>对象转数组<br><br>"; $arr=object_array($obj); var_dump($arr); echo "<br><br>输出数组数值<br><br>"; var_dump($arr["name"]); echo "<br>-------------------------------<br>"; echo "<br><br>json到数组<br><br>"; $json ='{"a":"哈哈","b":"我","c":"数","d":"一","e":3,"f":2,"g":1,"h":"别生气了"}'; $arr=simple_json_parser($json); var_dump($arr); echo "<br>官方换种方法<br>"; echo "对象<br>"; var_dump(json_decode($json)); $a=json_decode($json); echo "<br>属性:"; echo $a->a; echo "<br>数组<br>"; var_dump(json_decode($json,true)); echo "<br>-------------------------------<br>"; echo "<br><br>数组到json<br><br>"; var_dump($arr); echo "<br>"; var_dump(json_encode($json, json_hex_apos));
输出
-------------------------------
现在的数组
array(3) { ["name"]=> string(9) "李小龙" ["tel"]=> string(11) "11111111111" ["age"]=> string(2) "12" }
-------------------------------
数组转对象
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\"}""
ps:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:
在线json代码检验、检验、美化、格式化工具:
json在线格式化工具:
在线xml/json互相转换工具:
json代码在线格式化/美化/压缩/编辑/转换工具:
c语言风格/html/css/json代码格式化美化工具: