Compare 2-dimensional data sets based on a specified second level value
I would probably iterate through the original arrays and make them 1-dimensional... something like
foreach($array1 as $aV){
$aTmp1[] = $aV['ITEM'];
}
foreach($array2 as $aV){
$aTmp2[] = $aV['ITEM'];
}
$new_array = array_diff($aTmp1,$aTmp2);
Compare multidimensional arrays with array_diff
You could use array_udiff.
If will filter the first array, by comparing its elements to the elements of other arrays passed to array_udiff
using the given callback. When the callback returns 0
for a pair, that element is removed from the result.
$result = array_udiff($arr2, $arr1, function ($a, $b) {
return strcmp($a['email'], $b['email']);
});
Get the diff between two 2-dimensional arrays
I solved this as following,
$array1 = array( array( 'StudentId' => 1 ), array( 'StudentId' => 2 ) );
$array2 = array( array( 'StudentId' => 1 ));
foreach($array1 as $a=>$val){
if(in_array($val,$array2)){
unset($array1[$a]);
}
}
var_dump(array_values($array1));
How to compare two array with multiple element to find different
Use array_udiff instead of array_diff, check the demo
$arr_1=array(array('name'=>'john','sex'=>'male','grade'=>'9'),array('name'=>'Smith','sex'=>'male','grade'=>'11'));
$arr_2=array(array('name'=>'john','sex'=>'male','grade'=>'9'),array('name'=>'Smith','sex'=>'male','grade'=>'11'),array('name'=>'Kelly','sex'=>'female','grade'=>'10'));
function udiff($a, $b)
{
return ($name_diff = strcmp($a["name"],$b["name"])) ? $name_diff : (($sex_diff = strcmp($a["sex"],$b["sex"])) ? $sex_diff : (strcmp($a["grade"],$b["grade"])));
}
$arrdiff = array_udiff($arr_2, $arr_1, 'udiff');
print_r($arrdiff);
PHP find first difference in two multi-dim arrays
You should use array_diff() combined with array_column.
array_diff(array_column($Array_A, 'field'), array_column($Array_B, 'field'))
array_diff - returns difference between two arrays
array_column - returns one column from multidimensional array
If you want to have only one result then you can use array_shift() which will take the first element from the beginning of an array
f.e
$diff = array_diff(array_column($Array_A, 'field'), array_column($Array_B, 'field'));
$firstDifference = array_shift($diff);
Related Topics
Error Message "Strict Standards: Only Variables Should Be Passed by Reference"
Illegal String Offset Warning PHP
Parse Xml With Namespace Using Simplexml
How to Post Data in PHP Using File_Get_Contents
Can PHP Curl Retrieve Response Headers and Body in a Single Request
What Are the Best Practices For Avoiding Xss Attacks in a PHP Site
How to Update Code That Uses the Deprecated Each() Function
Among $_Request, $_Get and $_Post Which One Is the Fastest
Form Submit With Ajax Passing Form Data to PHP Without Page Refresh
Curl Error 60: Ssl Certificate: Unable to Get Local Issuer Certificate
"Keep Me Logged In" - the Best Approach
Run PHP Script as Daemon Process
Merge Two Arrays as Key Value Pairs in PHP
Unicode Character in PHP String
Use Global Variables in a Class