Как различить 2 многомерных массива? - PullRequest
3 голосов
/ 30 октября 2019

Я пытаюсь получить разницу двух многомерных массивов и вернуть массив без совпадений в другом массиве.

У меня здесь:

Массив 1:

Array ( 
    [0] => Array ( [Dashboard] => /LMS/Dashboard ) 
    [1] => Array ( [Notifications] => /LMS/Notifications ) 
    [2] => Array ( [Clients] => /LMS/Clients ) 
    [3] => Array ( [Penalties] => /LMS/Penalties ) 
    [4] => Array ( [Payments] => /LMS/Payments ) 
    [5] => Array ( [Profit] => /LMS/Profit ) 
    [6] => Array ( [Income] => /LMS/Income ) 
    [7] => Array ( [Outcome] => /LMS/Outcome ) 
    [8] => Array ( [Accounts] => /LMS/Accounts ) 
    [9] => Array ( [Collection] => ) 
    [10] => Array ( [Missed Payments] => ) 
    [11] => Array ( [Applied Penalties] => ) 
    [12] => Array ( [Group Names] => /LMS/Group-Names ) 
)

Массив 2:

Array ( 
    [0] => Array ( [Notifications] => /LMS/Notifications ) 
    [1] => Array ( [Clients] => /LMS/Clients ) 
    [2] => Array ( [Penalties] => /LMS/Penalties ) 
    [3] => Array ( [Payments] => /LMS/Payments ) 
    [4] => Array ( [Collection] => ) 
    [5] => Array ( [Missed Payments] => ) 
    [6] => Array ( [Applied Penalties] => ) 
)

Я хочу получить массив, которому нет совпадений в массиве 1.

Ответы [ 3 ]

3 голосов
/ 30 октября 2019

Вы можете использовать следующий код:

public static function check_diff_multi($array1, $array2){
        //Get all the Keys from the 2nd Array.
        $keys = array();
        foreach( $array2 as $key => $a ) {
            $keys = array_merge( $keys, array_keys( $a ));
        }

        //This will be the final array
        $result = array();

         //Loop through First Array
        foreach( $array1 as $key => $a ) {
            $firstKey = key($a);  //Get the Key
            if( in_array( $firstKey, $keys ) ) {  //If this key exists in the 2nd array, then continue
                continue;
            }
            $result[] = $array1[$key]; //This key is not present in 2nd array, so add to output.
        }
        return $result;
    }
0 голосов
/ 30 октября 2019
Please try to use like this 

$aray1 = Array (
[0] => Array ( [Notifications] => /LMS/Notifications )
[1] => Array ( [Clients] => /LMS/Clients )
[2] => Array ( [Penalties] => /LMS/Penalties )
[3] => Array ( [Payments] => /LMS/Payments )
[4] => Array ( [Collection] => )
[5] => Array ( [Missed Payments] => )
[6] => Array ( [Applied Penalties] => )
);

$aray2 = Array (
[0] => Array ( [Dashboard] => /LMS/Dashboard )
[1] => Array ( [Notifications] => /LMS/Notifications )
[2] => Array ( [Clients] => /LMS/Clients )
[3] => Array ( [Penalties] => /LMS/Penalties )
[4] => Array ( [Payments] => /LMS/Payments )
[5] => Array ( [Profit] => /LMS/Profit )
[6] => Array ( [Income] => /LMS/Income )
[7] => Array ( [Outcome] => /LMS/Outcome )
[8] => Array ( [Accounts] => /LMS/Accounts )
[9] => Array ( [Collection] => )
[10] => Array ( [Missed Payments] => )
[11] => Array ( [Applied Penalties] => )
[12] => Array ( [Group Names] => /LMS/Group-Names )
);



$printResult = get_diff_array($aray1, $aray2);

function get_diff_array($aray1, $aray2) {
    $arrayResult = [];
    foreach ($aray1 as $key => $val) {
        if (isset($aray2[$key])) {
            if (is_array($val) && $aray2[$key]) {
                $arrayResult[$key] = get_diff_array($val, $aray2[$key]);
            }
        } else {
            $arrayResult[$key] = $val;
        }
    }
    return $arrayResult;
}
0 голосов
/ 30 октября 2019

Пожалуйста, попробуйте этот код:

public static function diffArray($array1,$array2){
    $flag=0;
        foreach($array1 as $key1 => $value1){
            foreach ($array2 as $key2 => $value2) {
                if($key1==$key2)
                    $flag++;
            }
            if($flag==0)
                $array3[$key1]=$value1;
            $flag=0;
        }
    return $array3;
}

Ваш вывод будет:

Array
(
    [Dashboard] => /LMS/Dashboard
    [Profit] => /LMS/Profit
    [Income] => /LMS/Income
    [Outcome] => /LMS/Outcome
    [Accounts] => /LMS/Accounts
    [Group Names] => /LMS/Group-Names
)
...