如何在PHP中获得两个多维数组的差异?

时间:2016-01-25 06:00:19

标签: php arrays multidimensional-array

我希望得到两个多维arrys的区别,例如,

First Array:

Array
(
    [qtr_selected] => Array
        (
            [partner_q_m_p__2031] => Array
                (
                    [0] => q1
                    [1] => q2
                )

            [partner_q_m_p__2032] => Array
                (
                    [0] => q1
                )

        )

)

第二阵列:

 Array
(
    [qtr_completed] => Array
        (
            [partner_q_m_p__2031] => Array
                (
                    [0] => q1
                )

        )

)

我如何得到array1&的差异? array2如下所示:

   Array
(
    [qtr_final] => Array
        (
            [partner_q_m_p__2031] => Array
                (
                    [0] => q2
                )

            [partner_q_m_p__2032] => Array
                (
                    [0] => q1
                )

        )

)

尝试了array_diff()函数,但没有得到array1作为除了array2之外的差异,我想从中减去array2后得到array1。

3 个答案:

答案 0 :(得分:2)

只需制作像

这样的自定义功能
function check_diff($arr1, $arr2){
    $check = (is_array($arr1) && count($arr1)>0) ? true : false;
    $result = ($check) ? ((is_array($arr2) && count($arr2) > 0) ? $arr2 : array()) : array();
    if($check){
        foreach($arr1 as $key => $value){
            if(isset($result[$key])){
                $result[$key] = array_diff($value,$result[$key]);
            }else{
                $result[$key] = $value;
            }
        }
    }

    return $result;
}
$result['qtr_final'] = check_diff($a1['qtr_selected'],$a2['qtr_completed']);
print_r($result);

答案 1 :(得分:1)

尝试如下:

$a1 = Array
(
        'qtr_selected' => Array
        (
                'partner_q_m_p__2031' => Array
                (
                        '0' => 'q1',
                        '1' => 'q2',
                ),

                'partner_q_m_p__2032' => Array
                (
                        '0' => 'q1'
                )

        )

);

$a2 = Array
(
        'qtr_completed' => Array
        (
                'partner_q_m_p__2031' => Array
                (
                        '0' => 'q1'
                )

        )

);

$result['qtr_final'] = check_diff_multi($a1['qtr_selected'], 

$a2['qtr_completed']);
print '<pre>';
print_r($result);
print '</pre>';


function check_diff_multi($array1, $array2){
    $result = array();
    foreach($array1 as $key => $val) {
        if(isset($array2[$key])){
            if(is_array($val) && $array2[$key]){
                $result[$key] = check_diff_multi($val, $array2[$key]);
            }
        } else {
            $result[$key] = $val;
        }
    }

    return $result;
}

答案 2 :(得分:0)

您可以使用以下命令获得array1和array2的区别:

<?
// array 1
$array1['qtr_selected']['partner_q_m_p__2031'] = array('q1','q2');
$array1['qtr_selected']['partner_q_m_p__2032'] = array('q1');

// array 2
$array2['qtr_completed']['partner_q_m_p__2031'] = array('q1');

$removalArr = array();
foreach ($array2 as $key1 => $value1) {
    foreach ($value1 as $key2 => $value2) {
        $removalArr = $value2; // get last value of removal array
    }
}

$finalArr = array();
foreach ($array1 as $key1 => $value1) {
    foreach ($value1 as $key2 => $value2) {
        // check difference if available, 
        // if difference available use array_diff else use normal.
        $finalArr['qtr_final'][$key2] = (array_diff($value2,$removalArr) ? array_diff($value2,$removalArr) : $value2); 
    }
}

echo "<pre>";
print_r($finalArr);

?>

<强>结果:

Array
(
    [qtr_final] => Array
    (
        [partner_q_m_p__2031] => Array
        (
            [1] => q2
        )

        [partner_q_m_p__2032] => Array
        (
            [0] => q1
        )    
    )    
)