user1276567
user1276567

Reputation:

How to echo an array of associative arrays?

I have this code:

foreach($html->find('ul.results') as $article) {
            $item['date']     = $article->find('span.result_date', 0)->plaintext;
            $item['title']    = $article->find('a.result_title', 0);
            $item['text'] = $article->find('span.result_text', 0)->plaintext;
            $item['read'] = $article->find('a.read_more', 0);
            $articles[] = $item;
    }
        foreach ($articles as &$item) {
            while ($i < 5) {
                echo $item['date']."&nbsp;&nbsp;&nbsp;";
                echo $item['title'].'</br>';
                echo $item['text']."&nbsp;";
                echo $item['read'].'</br></br>';
            $i++;
        }
    }

And I am trying to echo the results. Right now the second foreach isn't doing anything. It is just displaying five of the same articles. The articles are in the format of: date, title, text, read more. I am trying to eco the first five $articles, but I can't find a proper way to do so that isn't print_r.

Upvotes: 0

Views: 299

Answers (1)

Xesued
Xesued

Reputation: 4167

The whole loop inside the for each will just loop on the first element 5 times, move to the next element and loops 5 times, etc.

Try something like this:

$i = 0;
foreach ($articles as $item){
    echo items stuff...
    $i++;
    if ($i == 5) break;
}

Upvotes: 1

Related Questions