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\"}""