产生奇怪输出的语句是
$response = '{"17366":{"title":"title1","content":"content1"},"22747":{"title":"title2","content":"content2"}}';
$result = json_decode($response, true);
foreach ($result as $document => $details) {
echo "Title : {$details['title']}, ";
echo "content : {$details['content']} ";
echo '<br>';
}
//prints, this one ok
//Title : title1, content : content1
//Title : title2, content : content2
但如果
$response = '{"title":"title1"}';
$result = json_decode($response, true);
foreach ($result as $document => $details) {
echo "Title : {$details['title']}, ";
echo "content : {$details['content']} ";
echo '<br>';
}
//prints
//Title : t, content : t
在这种情况下,我知道它$details
不是一个数组,并且它没有这样的键,如果是这样,它应该产生异常或错误。但它只打印这两个字符串的第一个字母。
任何人请指出我做错了什么?或者是这种行为,我没有断言什么?