Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.