1

我试图通过使用两个定界符爆炸来获得两个 foreach 循环<>"\n"但出现错误。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>';

?>

进入textarea的值是这样的Title here<>this is the first scooter ever made. Title here 2<>another line for specification事实上我想让它像<title 1> here detail text

非常感谢

4

3 回答 3

2

$descs变量不是数组,因为第一个 foreach 循环设置$descs.

看到这一行:

foreach($titles as $title => $descs){
于 2012-08-16T10:24:02.127 回答
2

实际上你应该做这样的事情

<?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>';

?>
于 2012-08-16T10:28:11.530 回答
1
$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>';
于 2012-08-16T10:26:27.910 回答