Compare 2-Dimensional Data Sets Based on a Specified Second Level Value

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



Leave a reply



Submit