I am building a forum system with the following structure: The categories displayed will depend on a $site variable, which I think is a red herring here but I include it anyway. The beginning of my code looks like this: $site = "games"; $myQuery = sprintf(" SELECT id_category, title FROM tbl_CATEGORY WHERE site='%s' ", $site ); $result = mysql_query($myQuery); $rows = 0; while($row = mysql_fetch_array($result)) { $categories[$rows] = $row; $rows++; } if ($rows < 1) { // No categories found echo 'This forum contains no categories.'; } else { // Categories found, details in $categories array foreach ($categories as $category) { echo '<b>' . $category['title'] . '</b><br />'; $myQuery = sprintf(" SELECT * FROM tbl_FOLDER WHERE category='%s' ", $category['id_category'] ); $result = mysql_query($myQuery); $rows = 0; while($row = mysql_fetch_array($result)) { $folders[$rows] = $row; $rows++; } var_dump($folders); echo '<br />'; //debug } unset($category); } Code (markup): var_dump($folders) gives the following output (which I've formatted for easy reading): array(3) { [0]=> array(8) { [0]=> string(1) "1" ["id_folder"]=> string(1) "1" [1]=> string(10) "New Folder 1" ["title"]=> string(10) "New Folder 1" [2]=> string(1) "1" ["category"]=> string(1) "1" [3]=> string(9) "games" ["site"]=> string(9) "games" } [1]=> array(8) { [0]=> string(1) "2" ["id_folder"]=> string(1) "2" [1]=> string(12) "New Folder 2" ["title"]=> string(12) "New Folder 2" [2]=> string(1) "1" ["category"]=> string(1) "1" [3]=> string(9) "games" ["site"]=> string(9) "games" } [2]=> array(8) { [0]=> string(1) "3" ["id_folder"]=> string(1) "3" [1]=> string(12) "New Folder 3" ["title"]=> string(12) "New Folder 3" [2]=> string(1) "1" ["category"]=> string(1) "1" [3]=> string(9) "games" ["site"]=> string(9) "games" } } array(3) { [0]=> array(8) { [0]=> string(1) "4" ["id_folder"]=> string(1) "4" [1]=> string(12) "New Folder 4" ["title"]=> string(12) "New Folder 4" [2]=> string(1) "2" ["category"]=> string(1) "2" [3]=> string(9) "games" ["site"]=> string(9) "games" } [1]=> array(8) { [0]=> string(1) "5" ["id_folder"]=> string(1) "5" [1]=> string(15) "New Folder 5" ["title"]=> string(15) "New Folder 5" [2]=> string(1) "2" ["category"]=> string(1) "2" [3]=> string(9) "games" ["site"]=> string(9) "games" } [2]=> array(8) { [0]=> string(1) "3" ["id_folder"]=> string(1) "3" [1]=> string(12) "New Folder 3" ["title"]=> string(12) "New Folder 3" [2]=> string(1) "1" ["category"]=> string(1) "1" [3]=> string(9) "games" ["site"]=> string(9) "games" } } Code (markup): The problem here is that New Folder 3 is being included in the results for both categories, despite clearly belonging to only category 1. The solution is probably really obvious and I just can't see the wood for the trees, so can anyone point out where I'm going wrong? Many thanks in advance!
In your code, on the section ... foreach ($categories as $category) { echo '<b>' . $category['title'] . '</b><br />'; Change it to: foreach ($categories as $category) { unset($folders); echo '<b>' . $category['title'] . '</b><br />'; Your problem is that on the first run, you fill the $folders [0], [1] and [2] On the second run, you ONLY MODIFY [0] and [1] So, the folder[2] remains the same as previously was set. REMEMBER: unset array vars when neccesary (loops are the first place to look for this unsets)