0

I'm trying to save two different tags content in two different variable in the same "foreach" loop, but I get parse error in the following:

foreach( $days -> find('td[!width]') as $schedule) && ($days -> find('a') as $title ) {
        echo "<div class=\"movie_name\">"
        .$title->plaintext."</div> - <div class=\"movie_schedule\">"
        .$schedule->plaintext."</div>\n";
}

Any ideas on what could be wrong?

* Edit *

I solved the problem separating the loop in two separate ones, like this:

    $movie_titles = array();
    foreach($days -> find('a') as $title) {
        $movie_titles[] = $title->plaintext;
    }
    $counter = 0;
    foreach( $days -> find('td[!width]') as $schedule) {
        echo "<div class=\"movie_name\">"
        .$movie_titles[$counter]
        ."</div> - <div class=\"movie_schedule\">"
        .$schedule->plaintext."</div>\n";
        $counter = $counter + 1;
    }

Not very elegant, but it does the job.

2
  • Share the exact error message you get instead of letting the community guess. Commented Mar 7, 2016 at 11:22
  • I was getting exactly parse error as error. Commented Mar 7, 2016 at 11:58

1 Answer 1

1

foreach doesn't work this way. Only

foreach ($array as $key => $value) {
    // ...
}

or

foreach ($array as $value) {
    // ...
}

You have to use two loops or totally different approach (depends on your task).

I.e.

foreach ($days->find('td[!width]') as $schedule) {
    foreach ($days->find('a') as $title) {
        echo "<div class=\"movie_name\">"
            . $title->plaintext."</div> - <div class=\"movie_schedule\">"
            . $schedule->plaintext."</div>\n";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion, I already figured it out, and made two separate loops.

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.