我有以下输出(通过链接),它显示了一些XML im生成的var_dump:
在页面的最底部,您将看到由此代码生成的一些输出:
foreach ($xml->feed as $entry) {
$title = $entry->title;
$title2 = $entry->entry->title;
}
echo $title;
echo $title2;
由于某种原因,$ title2只输出一次,其中有多个条目?
我使用$xml = simplexml_load_string($data);
创建xml。
答案 0 :(得分:0)
在foreach循环的每次迭代中,您为$ title和$ tile2重新赋值。循环结束后,只能访问最后指定的值 可能的替代方案:
// print/use the values within the loop-body
foreach ($xml->feed as $entry) {
$title = $entry->title;
$title2 = $entry->entry->title;
echo $title, ' ', $title2, "\n";
}
// append the values in each iteration to a string
$title = $title2 = '';
foreach ($xml->feed as $entry) {
$title .= $entry->title . ' ';
$title2 .= $entry->entry->title . ' ';
}
echo $title, ' ', $title2, "\n";
// append the values in each iteration to an array
$title = $title2 = array();
foreach ($xml->feed as $entry) {
$title[] = $entry->title;
$title2[] = $entry->entry->title;
}
var_dump($title, $title2);