0

我试图让我的数组在 foreach 循环中循环,但它一遍又一遍地循环同一个。我究竟做错了什么?

$classes = array('red', 'blue', 'yellow', 'green', 'black', 'purple', 'grey');
foreach($classes as $class){
foreach ($html->find('.yfnc_datamodoutline1 .yfnc_tabledata1') as $element){
    $symbols = $element->outertext;
    echo '<div class="'.$class.'">'.$symbols.'</div>';
}

它输出这个

<div class="red">Jul 6, 2012</div>
<div class="red">12,889.40</div>
<div class="red">12,889.40</div>
<div class="red">12,702.99</div>
<div class="red">12,772.47</div>
<div class="red">967,600</div>
<div class="red">12,772.47</div>

我试图让它输出这个

<div class="red">Jul 6, 2012</div>
<div class="blue">12,889.40</div>
<div class="yellow">12,889.40</div>
<div class="green">12,702.99</div>
<div class="black">12,772.47</div>
<div class="purple">967,600</div>
<div class="grey">12,772.47</div>
4

1 回答 1

3

您不能为此使用嵌套循环,而是使用颜色数组中的索引。

$classes = array('red', 'blue', 'yellow', 'green', 'black', 'purple', 'grey');
$ci = 0;
foreach ($html->find('.yfnc_datamodoutline1 .yfnc_tabledata1') as $element){
    $symbols = $element->outertext;
    echo '<div class="'.$classes[$ci].'">'.$symbols.'</div>';
    $ci++;
    if($ci == count($classes)) $ci = 0;
}
于 2012-07-10T17:42:35.540 回答