我想回显/打印一个JSON字符串,这是我尝试过的。
我已经解码,因为它返回了关联数组。
$decoded = json_decode('{"Title": "The Cuckoos Calling",
"Author": "Robert Galbraith",
"Detail": {
"Publisher": "Little Brown"
}}', true/* returns Associative Array */);
foreach ($decoded as $head => $inner) {
echo $head . ': </br>';
}
它只打印......
Title:
Author:
Detail:
这就是我想要的......
Expected Output :
Title : The Cuckoos Calling
Author : Robert Galbraith
修改
其print_r()
输出为。
Array ( [Title] => The Cuckoos Calling [Author] => Robert Galbraith [Detail] => Array ( [Publisher] => Little Brown ) )
var_dump()
是
array (size=3)
'Title' => string 'The Cuckoos Calling' (length=19)
'Author' => string 'Robert Galbraith' (length=16)
'Detail' =>
array (size=1)
'Publisher' => string 'Little Brown' (length=12)
答案 0 :(得分:4)
您是否注意到您只打印$键而不是值???
foreach ($decoded as $head => $inner) {
echo $head . ': '. $inner . '</br>';
}
这就是你需要的:
function arrayToUl($array)
{
$out = "<ul>";
foreach ($array as $key => $value) {
$value_string = $value;
if (is_array($value))
$value_string = arrayToUl($value);
$out .= "<li>{$key} : {$value_string}</li>";
}
$out .= "</ul>";
return $out;
}
echo arrayToUl($decoded);
答案 1 :(得分:1)
array_walk_recursive($decoded, function($key,$value) {
echo $value.' :'.$key.'<br>';
});
<强>输出强>:
Title :The Cuckoos Calling
Author :Robert Galbraith
Publisher :Little Brown