new: [utility:utils] Added utils file and support of arary_diff_recursive

develop-unstable
Sami Mokaddem 2022-12-02 09:50:09 +01:00
parent c49e3ac508
commit 813ec6f0a5
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
1 changed files with 32 additions and 0 deletions

32
src/Utility/Utils.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace App\Utility\Utils;
// src: https://www.php.net/manual/en/function.array-diff.php#91756
function array_diff_recursive($arr1, $arr2)
{
$outputDiff = [];
foreach ($arr1 as $key => $value) {
//if the key exists in the second array, recursively call this function
//if it is an array, otherwise check if the value is in arr2
if (array_key_exists($key, $arr2)) {
if (is_array($value)) {
$recursiveDiff = array_diff_recursive($value, $arr2[$key]);
if (count($recursiveDiff)) {
$outputDiff[$key] = $recursiveDiff;
}
} else if (!in_array($value, $arr2)) {
$outputDiff[$key] = $value;
}
}
//if the key is not in the second array, check if the value is in
//the second array (this is a quirk of how array_diff works)
else if (!in_array($value, $arr2)) {
$outputDiff[$key] = $value;
}
}
return $outputDiff;
}