I have this query
$categories = $dbh->query(" SELECT * FROM categories ORDER BY name ASC ");
and I need to loop two times over this array.
foreach($categories as $category) {
dd($category);
}
echo '---------------';
foreach($categories as $category) {
dd($category);
}
This code returns
stdClass Object
(
[id] => 24
[name] => granchi
[slug] => granchi
)
stdClass Object
(
[id] => 26
[name] => molluschi
[slug] => molluschi
)
stdClass Object
(
[id] => 25
[name] => pesci
[slug] => pesci
)
---------------
In the second loop the array becomes empty. I have found a similar question, Two While Loops, Second Returns Empty Array? , but I didn't find a similar solution for PDO.
Without querying two times creating two identical arrays, how do I loop through them twice ?
And I do not understand why it empties after the first loop.
dd()
do? – Mr. Llama Jul 21 '14 at 14:23