繁体   English   中英

我怎样才能比较像下面的二维数组?

[英]How i can compare two dimensional array like below?

我需要仅基于“值”键在数组2中不存在的数组1元素。

阵列1

$array1 = array(
     array('value' => 113214, 'revision_id' => 2047152),
     array('value' => 236462, 'revision_id' => 2045678),
     array('value' => 236541, 'revision_id' => 2047155)
);

阵列2

$array2 = array(
    array('value' => 113214, 'revision_id' => 2047152),
    array('value' => 236461, 'revision_id' => 2047153),
    array('value' => 236541, 'revision_id' => 2047155)
);

我需要以下输出,数组的差异应基于值

$output = array(
    array('value' => 236462, 'revision_id' => 2045678)
);

只需做一个嵌套的foreach循环并检查条件,希望它可以帮助您:

$arraycheck= array();

foreach($newData as $data1) {

  $duplicatecheck = false;
  foreach($oldData as $data2) {
    if($data1['value'] === $data2['value'] && $data1['revision_id'] === $data2['revision_id']) $duplicatecheck = true;
  }

  if($duplicatecheck === false) $arraycheck[] = $data1;
}

您可以使用array_udiff接受最后一个参数作为回调,并且可以在那里轻松定义比较。

$array1 = [
    ['value' => '113214', 'revision_id' => '2047152'],
    ['value' => '236462', 'revision_id' => '2045678'],
    ['value' => '236541', 'revision_id' => '2047155'],
];
$array2 = [
    ['value' => '113214', 'revision_id' => '2047152'],
    ['value' => '236461', 'revision_id' => '2047153'],
    ['value' => '236541', 'revision_id' => '2047155'],
];

$result = array_udiff ($array1, $array2, function($x, $y) {
    return $x['value'] - $y['value'];
});
print_r($result);

首先,使用array_column将来自array2的“值”的值转换为一维数组:

$a2values = array_column($array2, 'value');

然后将这些值用于array_filter array1。

$result = array_filter($array1, function($item) use ($a2values) {
    // only keep items with values not in array2
    return !in_array($item['value'], $a2values);
});

摘自: https : //gist.github.com/wrey75/c631f6fe9c975354aec7

function my_array_diff($arr1, $arr2) {
    $diff = array();

    // Check the similarities
    foreach( $arr1 as $k1=>$v1 ){
        if( isset( $arr2[$k1]) ){
            $v2 = $arr2[$k1];
            if( is_array($v1) && is_array($v2) ){
                // 2 arrays: just go further...
                // .. and explain it's an update!
                $changes = self::diff($v1, $v2);
                if( count($changes) > 0 ){
                    // If we have no change, simply ignore
                    $diff[$k1] = array('upd' => $changes);
                }
                unset($arr2[$k1]); // don't forget
            }
            else if( $v2 === $v1 ){
                // unset the value on the second array
                // for the "surplus"
                unset( $arr2[$k1] );
            }
            else {
                // Don't mind if arrays or not.
                $diff[$k1] = array( 'old' => $v1, 'new'=>$v2 );
                unset( $arr2[$k1] );
            }
        }
        else {
            // remove information
            $diff[$k1] = array( 'old' => $v1 ); 
        }
    }

    // Now, check for new stuff in $arr2
    foreach( $arr2 as $k=>$v ){
        // OK, it is quite stupid my friend
        $diff[$k] = array( 'new' => $v );
    }
    return $diff;
}

用法:

$diff = my_array_diff($arr1, $arr2);
var_dump($diff);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM