我试图通过爆炸两个分隔符<>
和"'n"
来获得两个foreach循环,但得到错误。 Warning: Invalid argument supplied for foreach()
这是我的代码
<?php
$specifications = $scooter_meta->get_the_value('specifications');
$titles = explode('<>', $specifications);
$descs = explode("'n", $specifications);
echo '<dl>';
foreach($titles as $title => $descs){
echo '<dt>' . $title . '</dt>';
foreach($descs as $desc){
echo '<dd>' . $desc . '</dd>';
}
}
echo '</dl>';
?>
输入文本区域的值如下所示Title here<>this is the first scooter ever made.
Title here 2<>another line for specification
实际上我想让它像<title 1> here detail text
多谢
实际上你应该做这样的事情
<?php
$specifications = $scooter_meta->get_the_value('specifications');
$descs = explode("'n", $specifications);
echo '<dl>';
foreach($descs as $desc){
$title = explode('<>', $desc);
echo '<dt>' . $title[0] . '</dt>';
for($i=1; $i<=count($title); $i++){
echo '<dd>' . $title[$i] . '</dd>';
}
}
echo '</dl>';
?>
$descs
变量不是数组,因为第一个 foreach 循环设置$descs
。
请参阅此行:
foreach($titles as $title => $descs){
$specifications = $scooter_meta->get_the_value('specifications');
$titles = explode('<>', $specifications);
echo '<dl>';
foreach($titles as $title => $descs){
echo '<dt>' . $title . '</dt>';
$descs = explode("'n", $descs);
foreach($descs as $desc){
echo '<dd>' . $desc . '</dd>';
}
}
echo '</dl>';