Jump to content

array_diff failing


kingnutter

Recommended Posts

 

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);

Link to comment
https://forums.phpfreaks.com/topic/174349-array_diff-failing/
Share on other sites

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 )

Link to comment
https://forums.phpfreaks.com/topic/174349-array_diff-failing/#findComment-919043
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.