kingnutter Posted September 15, 2009 Share Posted September 15, 2009 I cannot work out why the code below is failing. I have a test section (Group A & B stuff) running to check the array_diff function which works fine. But for the real thing $missing_genres is always wrong. This is the output: Group A: Array ( [0] => Elton [1] => Jagger [2] => Bowie ) Group B:Array ( [0] => Lennon [1] => Bowie [2] => Elton ) Missing from Group A: Array ( [1] => Jagger ) Added in Group B: Array ( [0] => Lennon ) Original Genres: Array ( [0] => Rock [1] => Pop [2] => Psych [3] => Mod [4] => Rock [5] => Pop [6] => Psych [7] => Mod ) New Genres: Array ( [0] => Rock [1] => Pop [2] => Psych [3] => Cheese ) Missing Genres: Array ( [1] => Pop [2] => Psych [3] => Mod [5] => Pop [6] => Psych [7] => Mod ) Where are those two rogue values at the beginning of the final array coming from? I am unsetting all the variables to start with. What could be wrong? unset($orig_genres); unset($new_genres); unset($missing_genres); foreach (($_SESSION['tags']) as $single_genre) { $orig_genres[] = $single_genre; } unset($_SESSION['tags']); // Create new_genres string from user input ($moj_genre) set earlier in code $new_genres = explode(",", $moj_genre); //test bit: This works fine $group_a = Array ('Elton', 'Jagger', 'Bowie'); $group_b = Array ('Lennon', 'Bowie', 'Elton'); echo "Group A: "; print_r($group_a); ?><br><?php echo "Group B:"; print_r($group_b); ?><br><?php $missing=array_diff($group_a, $group_b); echo "Missing from Group A: "; print_r($missing); ?><br><?php $added=array_diff($group_b, $group_a); echo "Added in Group B: "; print_r($added); ?> <br /> <?php //Actual code: Note that all variables have been unset but this still fails echo "Original Genres: "; print_r($orig_genres); ?><br><?php echo "New Genres: "; print_r($new_genres); ?><br><?php $missing_genres=array_diff($orig_genres, $new_genres); echo"Missing Genres: "; print_r($missing_genres); Quote Link to comment https://forums.phpfreaks.com/topic/174349-array_diff-failing/ Share on other sites More sharing options...
mikesta707 Posted September 15, 2009 Share Posted September 15, 2009 it probably has something to do with the fact that pop, psyche and mod both appear twice in your original array. try using the array_unique function on both the missing and orig genres arrays before you array_diff them Quote Link to comment https://forums.phpfreaks.com/topic/174349-array_diff-failing/#findComment-919038 Share on other sites More sharing options...
kingnutter Posted September 15, 2009 Author Share Posted September 15, 2009 That example demonstrates the problem. My arrays seem to be building up. I've refreshed my browser and it still occurs: Group A: Array ( [0] => Elton [1] => Jagger [2] => Bowie ) Group B:Array ( [0] => Lennon [1] => Bowie [2] => Elton ) Missing from Group A: Array ( [1] => Jagger ) Added in Group B: Array ( [0] => Lennon ) Original Genres: Array ( [0] => Rock [1] => Pop [2] => Psych [3] => Mod ) New Genres: Array ( [0] => Rock [1] => Pop [2] => Psych [3] => Soul ) Missing Genres: Array ( [1] => Pop [2] => Psych [3] => Mod ) Quote Link to comment https://forums.phpfreaks.com/topic/174349-array_diff-failing/#findComment-919043 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.