Created
April 30, 2022 18:17
-
-
Save Lysak/7613171693ffb2477869ea1b87e64f2f to your computer and use it in GitHub Desktop.
array_diff_multidimensional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function array_diff_multidimensional($arr1, $arr2): array | |
{ | |
$check = is_array($arr1) && count($arr1) > 0; | |
$result = ($check) ? ((is_array($arr2) && count($arr2) > 0) ? $arr2 : []) : []; | |
if ($check) { | |
foreach ($arr1 as $key => $value) { | |
if (isset($result[$key])) { | |
$result[$key] = array_diff($value, $result[$key]); | |
} else { | |
$result[$key] = $value; | |
} | |
} | |
} | |
return $result; | |
} |
Author
Lysak
commented
Apr 30, 2022
•
/**
* @param array|null $array1
* @param array|null $array2
* @return array
*/
public function array_diff_multidimensional(?array $array1, ?array $array2): array
{
if (!is_array($array1)) {
throw new InvalidArgumentException('$array1 must be an array!');
}
if (!is_array($array2)) {
return $array1;
}
$check = count($array1) > 0;
$result = ($check) ? ((count($array2) > 0) ? $array2 : []) : [];
if ($check) {
foreach ($array1 as $key => $value) {
if (isset($result[$key])) {
$result[$key] = array_diff($value, $result[$key]);
} else {
$result[$key] = $value;
}
if (empty($result[$key])) {
unset($result[$key]);
}
}
}
return $result;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment