search

Home  >  Q&A  >  body text

How to determine whether multi-dimensional unordered arrays are the same in PHP?

The example is as follows:
The actual operation is a three-dimensional array, and the amount of data is relatively large.


    $a = [
        ['name'=>'jack', 'gender'=>'male'],
        ['age'=>18,'country'=>'China']
    ];
    $b = [
        ['country'=>'China','age'=>18],
        ['gender'=>'male','name'=>'jack']
    ];

In addition to traversing and comparing, is there any quick and effective way?
My current idea is to sort the array according to the same rules first, and then use md5 verification after json_encoding. However, it seems that sorting cannot be implemented well at present.
I wonder if you have any good methods?

PHPzPHPz2773 days ago497

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-05-16 13:14:54

    //Only applicable to purely associative arrays
    function deep_ksort(&$arr) {

    ksort($arr); 
    foreach ($arr as &$a) { 
        if (is_array($a) && !empty($a)) { 
            deep_ksort($a); 
        } 
    } 

    }
    deep_ksort($a);
    deep_ksort($b);
    if(json_encode($a) == json_encode($b)){

        echo "两个数组相同";

    }


    //General method
    //This function converts a multi-dimensional array into a one-dimensional array
    function multiToSingle($arr, $delimiter = '->',$key = ' ') {

    $resultAry = array();
    if (!(is_array($arr) && count($arr)>0)) {
        return false;
    }
    foreach ($arr AS $k=>$val) {
        $k = is_numeric($k) ? 0 :$k;    //若键是数字,则统一为0,避免索引不同导致数组的不同
        $newKey = trim($key . $k . $delimiter);
        if (is_array($val) && count($val)>0) {
            $resultAry = array_merge($resultAry, multiToSingle($val, $delimiter, $newKey));
        } else {
            $resultAry[] = $newKey . $val;
        }
    }
    return $resultAry;

    }
    //Judge whether two arrays are congruent
    function judge($a,$b)
    {

    $single_A = multiToSingle($a);
    $single_B = multiToSingle($b);
    $arr1 = array_diff($single_A,$single_B);
    $arr2 = array_diff($single_B,$single_A);

    if(empty($arr1) && empty($arr2)){

       echo "两数组全等";

    }else{

       echo "不全等";

    };

    }

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:14:54

    You can take a look at this http://bbs.csdn.net/topics/36..., it should be useful to you!

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:14:54

    It’s very simple. After sorting by the same rules, convert the array into a string and compare whether the two strings are the same

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:14:54

    
    $a = [
        ['name' => 'jack', 'gender' => 'male'],
        ['age' => 18, 'country' => 'China']
    ];
    $b = [
        ['country' => 'China', 'age' => 18],
        ['gender' => 'male', 'name' => 'jack']
    ];
    
    
    function whetherDifferent($ele, $differentEle)
    {
        $toSingleArr = function ($target)use(&$toSingleArr){
                //转换为一维数组的代码
            return $target;
        };
        $ele=$toSingleArr($ele);
        $differentEle=$toSingleArr($differentEle);
        ksort($ele,true);
        ksort($differentEle,true);
        return $ele===$differentEle;
    }
    
    
    var_dump(whetherDifferent($a, $b));
    die;
    

    reply
    0
  • Cancelreply