AdRock Posted October 31, 2013 Share Posted October 31, 2013 (edited) I have this array which has been pulled from a MySQL database (did a var_dump) array(2) { [0]=> array( { ["id"]=> string(1) "3" [0]=> string(1) "3" ["title"]=> string(23) "New Venture Coming Soon" [1]=> string(23) "New Venture Coming Soon" ["archived"]=> string(1) "y" [2]=> string(1) "y" ["postdate"]=> string(14) "7th March 2009" [3]=> string(14) "7th March 2009" } [1]=> array( { ["id"]=> string(1) "4" [0]=> string(1) "4" ["title"]=> string(22) "Visit To Headley Court" [1]=> string(22) "Visit To Headley Court" ["archived"]=> string(1) "y" [2]=> string(1) "y" ["postdate"]=> string(14) "7th March 2009" [3]=> string(14) "7th March 2009" } } I want to loop through the array and display the information but if $not_archived equals to zero then it displays a message. The problem is that it displays the message becuase $not_archived is equal to zero $z = 0; $not_archived = 0; foreach($news as $newsitem) { if($newsitem['archived'] == 'N') { if($z % 2==0) { //<tr class="yellow"> $z++; } else { //<tr class="white"> $z++ } $not_archived++; echo $newsitem['id']; echo $newsitem['title']; } } if($not_archived == 0) { //No active news in database } Edited October 31, 2013 by AdRock Quote Link to comment https://forums.phpfreaks.com/topic/283463-looping-through-an-array-of-arrays-with-a-foreach-loop/ Share on other sites More sharing options...
Ch0cu3r Posted October 31, 2013 Share Posted October 31, 2013 (edited) Because you are only incrementing the $not_archived variable when the current news item's archived field is set to N ($newsitem['archived']). The var_dump you posted contains two news items and both have the archived field set to y. Edited October 31, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283463-looping-through-an-array-of-arrays-with-a-foreach-loop/#findComment-1456341 Share on other sites More sharing options...
Solution AdRock Posted October 31, 2013 Author Solution Share Posted October 31, 2013 I just found that. Thanks Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283463-looping-through-an-array-of-arrays-with-a-foreach-loop/#findComment-1456342 Share on other sites More sharing options...
Barand Posted October 31, 2013 Share Posted October 31, 2013 Why don't you query for those WHERE archive = 'N' ? If you have to store in array, use fetch_assoc() and not fetch_array(). As you may have noticed, fetch_array() gets all the values twice. EG while ($row = $db->fetch_assoc()) { $array[] = $row; } Quote Link to comment https://forums.phpfreaks.com/topic/283463-looping-through-an-array-of-arrays-with-a-foreach-loop/#findComment-1456345 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.