php - if/else if function based on the id name of a div that has a certain class name?

php - if/else if function based on the id name of a div that has a certain class name?

已更新:

所以我尝试改变一些东西,但我坚持不下去了。

            <?php
        $array = [
            'id01' => [
                'class' => 'yellow',
            ],
            'id02' => [
                'class' => 'blue',
            ],
            'id03' => [
                'class' => 'yellow',
            ],
            'id04' => [
                'class' => 'yellow',
            ],
            'id05' => [
                'class' => 'yellow',
            ],
        ];

        foreach ($array as $key => $value) {
            if ($key == 'id01') {
                $vrnr = "1";
                $text = "Some text here";
            } elseif ($key == 'id02') {
                $vrnr = "2";
                $text = "lala";
            } elseif ($key == 'id03') {
                $vrnr = "2";
                $text = "bobobo";
            } elseif ($key == 'id04') {
                $vrnr = "2";
                $text = "testtest";
            } elseif ($key == 'id05') {
                $vrnr = "2";
                $text = "another one here";
            }
            echo '<div id="' . $key . '" class="' . $value['class'] . '">' . $text . '</div>';
        }
        ?>


<div id="id01" class="blue"><?=$text?><?=$vrnr?></div>

<div id="id01" class="yellow"><?=$text?><?=$vrnr?></div>
<div id="id02" class="yellow"><?=$text?><?=$vrnr?></div>
<div id="id03" class="yellow"><?=$text?><?=$vrnr?></div>
<div id="id04" class="yellow"><?=$text?><?=$vrnr?></div>
<div id="id05" class="yellow"><?=$text?><?=$vrnr?></div>

作为输出:

Some text here
lala
bobobo
testtest
another one here
another one here2
another one here2
another one here2
another one here2
another one here2
another one here2

为什么代码在读取identiaue div id name 后只使用elseif ($key == 'id05') { 行输出?

原题:

想象一下这些 div 在我的 html:

<div id="id01" class="blue"><?=$text?></div>

<div id="id01" class="yellow"><?=$text?></div>
<div id="id02" class="yellow"><?=$text?></div>
<div id="id03" class="yellow"><?=$text?></div>
<div id="id04" class="yellow"><?=$text?></div>
<div id="id05" class="yellow"><?=$text?></div>

<-- a hundred more divs after this-->

有了 php,我怎么才能找到唯一的 div id 只有 class 命名为 yellow 并把它变成一个变量来给它 if/else 如果功能正常?

$divId = #divID.yellow

if ($divId == id01) {
    $text = "Some text here";
} elseif ($divId== id02) {
    $text = "another phrase here";


// a hundred more if statements

所以我得到 html 输出:

<div id="id01" class="yellow">Some text here</div>
<div id="id02" class="yellow">another phrase here</div>

与其将 div 1 对 1 地构建,不如将信息存储在一个数组中并循环遍历它以将每个信息作为 div 打印出来。这样你就可以将你的 id 存储在数组中并应用你的 if/else 语句。

例如...

        <?php
        $array = [
            'id01' => [
                'class' => 'yellow',
            ],
            'id02' => [
                'class' => 'blue',
            ],
            'id03' => [
                'class' => 'yellow',
            ],
        ];

        foreach ($array as $key => $value) {
            if ($key == 'id01') {
                $text = "Some text here";
            } elseif ($key == 'id02') {
                $text = "another phrase here";
            }
            echo '<div id="' . $key . '" class="' . $value['class'] . '">' . $text . '</div>';
        }
        ?>