$arr = array("name" => 'qssq', "age" => 18);
foreach ($arr as $key => $value) {
echo "key: $key,value:$value";
}
//$json=json_decode($jsonStr);//json字符串轉(zhuǎn)json對象
$json= json_decode(json_encode($arr));//arr數(shù)組轉(zhuǎn)json字符串再轉(zhuǎn)對象
$type=gettype($json);
echo "type: $type \n";
foreach ($json as $key => $value) {
echo "key: $key,value:$value";
}
輸出結(jié)果
D:\phpStudy\php\php-7.0.12-nts\php.exe D:\phpStudy\WWW\tp5\client\test.php
key: name,value:qssqkey: age,value:18type: object
key: name,value:qssqkey: age,value:18
Process finished with exit code 0
對象 或者json對象轉(zhuǎn)數(shù)組
//PHP stdClass Object轉(zhuǎn)array
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;
}