本文介绍了如何在php中获得两个多维数组的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解两个多维项目之间的区别,例如,

I want to get the difference of two multidimensional arrys, e.g.,

第一个数组:

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如下所示:

how do i get the difference of array1 & array2 as given below:

   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.

Tried array_diff() function but not getting array1 as difference except array2, i want array1 after subtracting array2 from it.

推荐答案

只需创建一个自定义函数,如

Simply make a custom function like as

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);

这篇关于如何在php中获得两个多维数组的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 10:26