我试图让我的数组在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>
答案 0 :(得分: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;
}