Home > Net >  How to find the difference between two multidimensional arrays with different sizes and levels in PH
How to find the difference between two multidimensional arrays with different sizes and levels in PH

Time:01-13

I have two multidimensional arrays in php with different sizes and levels. I need to find the elements in the second array, that are not in the first one.

Array A:

$bmw_a = array(
    "3 series" => array(),
    "5 series" => array(
        "520D" => array(
            "N47_163" => array(),
        ),
        "530i" => array(),
    ),
    "7 series" => array(),
);

Array B:

$bmw_b = array(
    "1 series" => array(),
    "3 series" => array(),
    "5 series" => array(
        "520D" => array(
            "N47_163" => array(),
            "M47_177" => array()
        ),
        "530i" => array(
            "M54_228" => array(),
            "N52_255" => array()
        ),
    ),
    "7 series" => array(),
);

I need the output to look something like this:

$output = array(
    "1 series" => array(),
    "5 series" => array(
        "520D" => array(
            "M47_177" => array()
        ),
        "530i" => array(
            "M54_228" => array(),
            "N52_255" => array()
        )
    )
);

I tried array_diff AND array_diff_assoc but non of these worked.

CodePudding user response:

array_diff_assoc() works only on one level, so for multidimensional arrays you need recursion.

Here it is:

<?php

$bmw_a = array(
    "3 series" => array(),
    "5 series" => array(
        "520D" => array(
            "N47_163" => array(),
        ),
        "530i" => array(),
    ),
    "7 series" => array(),
);

$bmw_b = array(
    "1 series" => array(),
    "3 series" => array(),
    "5 series" => array(
        "520D" => array(
            "N47_163" => array(),
            "M47_177" => array()
        ),
        "530i" => array(
            "M54_228" => array(),
            "N52_255" => array()
        ),
    ),
    "7 series" => array(),
);

$result = array_diff_assoc_recursive($bmw_b, $bmw_a);

echo '<pre>';
var_dump($result);

function array_diff_assoc_recursive($array1, $array2) {
    $diff = array();
    foreach ($array1 as $key => $value) {
        if (is_array($value)) {
            if (!isset($array2[$key]) || !is_array($array2[$key])) {
                $diff[$key] = $value;
            } else {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if (!empty($new_diff)) {
                    $diff[$key] = $new_diff;
                }
            }
        } elseif (!isset($array2[$key]) || $array2[$key] != $value) {
            $diff[$key] = $value;
        }
    }
    return $diff;
}

Output:

array(2) {
  ["1 series"]=>
  array(0) {
  }
  ["5 series"]=>
  array(2) {
    ["520D"]=>
    array(1) {
      ["M47_177"]=>
      array(0) {
      }
    }
    ["530i"]=>
    array(2) {
      ["M54_228"]=>
      array(0) {
      }
      ["N52_255"]=>
      array(0) {
      }
    }
  }
}
  • Related