glenelkins Posted October 28, 2009 Share Posted October 28, 2009 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! Link to comment https://forums.phpfreaks.com/topic/179350-array_diff-saying-no-difference-when-there-is/ Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 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. Link to comment https://forums.phpfreaks.com/topic/179350-array_diff-saying-no-difference-when-there-is/#findComment-946283 Share on other sites More sharing options...
glenelkins Posted October 28, 2009 Author Share Posted October 28, 2009 ok thanks. do you know , to save me looking it up if there is a function to check if there are 1 or more items from the first array, in the second? Link to comment https://forums.phpfreaks.com/topic/179350-array_diff-saying-no-difference-when-there-is/#findComment-946289 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 just reverse the array_diff() parameters. are you looking for something like this? array_merge(array_diff($array1, $array2), array_diff($array2, $array1)) Link to comment https://forums.phpfreaks.com/topic/179350-array_diff-saying-no-difference-when-there-is/#findComment-946294 Share on other sites More sharing options...
glenelkins Posted October 28, 2009 Author Share Posted October 28, 2009 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 Link to comment https://forums.phpfreaks.com/topic/179350-array_diff-saying-no-difference-when-there-is/#findComment-946305 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 maybe this? if(count(array_diff($array1, $array2))==count($array1)){ // if none of the elements in $array1 are present in array2 return true; } Link to comment https://forums.phpfreaks.com/topic/179350-array_diff-saying-no-difference-when-there-is/#findComment-946338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.