本文實例講述了PHP數(shù)組對象與Json轉(zhuǎn)換操作。分享給大家供大家參考,具體如下:
代碼
?php
//數(shù)組轉(zhuǎn)對象
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;
}
//對象轉(zhuǎn)數(shù)組
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;
}
//對象轉(zhuǎn)數(shù)組
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到數(shù)組
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 "現(xiàn)在的數(shù)組br>br>";
$arr=array(
"name"=>"李小龍",
"tel"=>"11111111111",
"age"=>"12",
);
var_dump($arr);
echo "br>-------------------------------br>";
echo "br>br>數(shù)組轉(zhuǎn)對象br>br>";
$obj= arrayToObject($arr);
var_dump($obj);
echo "br>br>輸出對象屬性br>br>";
var_dump($obj->name);
echo "br>-------------------------------br>";
echo "br>br>對象轉(zhuǎn)數(shù)組br>br>";
$arr=object_array($obj);
var_dump($arr);
echo "br>br>輸出數(shù)組數(shù)值br>br>";
var_dump($arr["name"]);
echo "br>-------------------------------br>";
echo "br>br>Json到數(shù)組br>br>";
$json ='{"a":"哈哈","b":"我","c":"數(shù)","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>數(shù)組br>";
var_dump(json_decode($json,true));
echo "br>-------------------------------br>";
echo "br>br>數(shù)組到Jsonbr>br>";
var_dump($arr);
echo "br>";
var_dump(json_encode($json, JSON_HEX_APOS));
輸出
-------------------------------
現(xiàn)在的數(shù)組
array(3) { ["name"]=> string(9) "李小龍" ["tel"]=> string(11) "11111111111" ["age"]=> string(2) "12" }
-------------------------------
數(shù)組轉(zhuǎn)對象
object(stdClass)#1 (3) { ["name"]=> string(9) "李小龍" ["tel"]=> string(11) "11111111111" ["age"]=> string(2) "12" }
輸出對象屬性
string(9) "李小龍"
-------------------------------
對象轉(zhuǎn)數(shù)組
array(3) { ["name"]=> string(9) "李小龍" ["tel"]=> string(11) "11111111111" ["age"]=> string(2) "12" }
輸出數(shù)組數(shù)值
string(9) "李小龍"
-------------------------------
Json到數(shù)組
array(8) { ["a"]=> string(6) "哈哈" ["b"]=> string(3) "我" ["c"]=> string(3) "數(shù)" ["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) "數(shù)" ["d"]=> string(3) "一" ["e"]=> int(3) ["f"]=> int(2) ["g"]=> int(1) ["h"]=> string(12) "別生氣了" }
屬性:哈哈
數(shù)組
array(8) { ["a"]=> string(6) "哈哈" ["b"]=> string(3) "我" ["c"]=> string(3) "數(shù)" ["d"]=> string(3) "一" ["e"]=> int(3) ["f"]=> int(2) ["g"]=> int(1) ["h"]=> string(12) "別生氣了" }
-------------------------------
數(shù)組到Json
array(8) { ["a"]=> string(6) "哈哈" ["b"]=> string(3) "我" ["c"]=> string(3) "數(shù)" ["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代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
C語言風格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP中json格式數(shù)據(jù)操作技巧匯總》、《PHP數(shù)學運算技巧總結(jié)》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- PHP數(shù)組基本用法與知識點總結(jié)
- PHP 數(shù)組操作詳解【遍歷、指針、函數(shù)等】
- php數(shù)組指針函數(shù)功能及用法示例
- PHP查找一列有序數(shù)組是否包含某值的方法
- php數(shù)組函數(shù)array_push()、array_pop()及array_shift()簡單用法示例
- PHP字符串與數(shù)組處理函數(shù)用法小結(jié)
- PHP實現(xiàn)二維數(shù)組(或多維數(shù)組)轉(zhuǎn)換成一維數(shù)組的常見方法總結(jié)
- PHP實現(xiàn)提取多維數(shù)組指定一列的方法總結(jié)
- php實現(xiàn)快速對二維數(shù)組某一列進行組裝的方法小結(jié)
- 如何在PHP中使用數(shù)組