0

Im trying to get my array to loop in the foreach loop but it keeps looping the same one over and over. What am I doing wrong?

$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>';
}

it outputs this

<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>

Im trying to make it output this

<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>

1 Answer 1

3

You can't use nested loops for this, instead use an index in the color array.

$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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I really appreciate it, I couldnt understand why it wasnt working since it was in a foreach loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.