Jump to content

array_diff saying no difference when there is


glenelkins

Recommended Posts

hi

 

i got 2 arrays:

 

$array1 = array ( '0' => 'gary' );
$array2 = array ( '0' => 'gary', [1] => 'bakewell' );

if ( !array_diff ( $array1, $array2 ) ) {
   die ( 'no difference' );
} else {
    die('different');
}

 

its coming back saying the arrays are no different, when they clearly are!

According to PHP.net, the array_diff function "Returns an array containing all the entries from array1 that are not present in any of the other arrays."

 

You will need to reverse array2 and array1 to see any visible results.

what i want

 

$array1 = array ( '0' => 'gary', [1] => 'someone', [2] => 'bakewell' );

$array2 = array ( '0' => 'gary', [1] => 'bakewell' );

 

$array3 = array ( '0' => 'gary' );

$array4 = array ( '0' => 'bakewell', [1] => 'gary' );

 

is to see if any of the items in array1  appear in array 2 but not an exact match. So in this case "gary" and "bakewell" are in both arrays so it will return true.

 

Or see if any items in $array3 are in $array4, so in this case true again because gary is in both

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.