shanejeffery86 Posted November 26, 2008 Share Posted November 26, 2008 Hey all. I have two different queries that I am running and I need to compare the results and join them together if they are the same. I originally had the query setup to do a series of LEFT JOINs, but due to some changes in what needs to be displayed, that is no longer an option, and the result is two queries. Both of the queries are being executed and all of the data put into arrays. foreach($rows as $row) { foreach($speaker_array as $speaker) { if($row['entry_id'] == $speaker['entry_id']) { array_push($row['speakers'], $speaker); } } } The data from the first query is in the array "$speaker_array". The data from the second query is in the array "$rows". What needs to happen is this: I need to loop through each $row record in the $rows array. The FOREACH statement is taking care of that. Then I need to go through each $speaker in the $speaker_array and compare the data in each element against a field in the $row. If there is a match, then I need to CONCAT (or in this case, array_push) the data that is in the $speaker array into the current iteration of $row. Since there can be multiple matches for the IF statement, I decided to create a new array $row['speakers'] inside of $row. When I do a print_r on the $rows array after this FOREACH clause ends, the $speaker data is not pushed onto ANY of the iterations of $row. Any idea what I am doing wrong here? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/134376-php5-foreacharray-push-issue/ Share on other sites More sharing options...
zq29 Posted November 26, 2008 Share Posted November 26, 2008 Have you looked into using array_intersect() ? It might help you out. Quote Link to comment https://forums.phpfreaks.com/topic/134376-php5-foreacharray-push-issue/#findComment-699710 Share on other sites More sharing options...
sasa Posted November 26, 2008 Share Posted November 26, 2008 try <?php foreach($rows as $k => $row) { foreach($speaker_array as $speaker) { if($row['entry_id'] == $speaker['entry_id']) { //array_push($row['speakers'], $speaker); $rows[$k]['speakers'][] = $speaker; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134376-php5-foreacharray-push-issue/#findComment-699716 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.