is_assoc($array1) || $this->is_assoc($array2))) { return array_values(array_diff($array1, $array2)); } foreach ($array1 as $array1key => $array1value) { if (array_key_exists($array1key, $array2)) { if (is_array($array1value) && is_array($array2[$array1key])) { $outputDiff[$array1key] = $this->arrayDiffRecursive($array1value, $array2[$array1key]); } else { throw new RuntimeException('Inconsistent data'); } } else { $outputDiff[$array1key] = $array1value; } } $result = []; foreach ($outputDiff as $i => $key) { if (!empty($key)) { $result[$i] = $key; } } return $result; } protected function arrayIntersectRecursive(array $array1, array $array2): array { $return = []; if (!($this->is_assoc($array1) || $this->is_assoc($array2))) { return array_intersect($array1, $array2); } $commonKeys = array_intersect(array_keys($array1), array_keys($array2)); foreach ($commonKeys as $key) { if (is_array($array1[$key]) && is_array($array2[$key])) { $intersection = $this->arrayIntersectRecursive($array1[$key], $array2[$key]); if ($intersection) { $return[$key] = $intersection; } } else if ($array1[$key] === $array2[$key]) { $return[$key] = $array1[$key]; } } return $return; } private function is_assoc(array $array): bool { return count(array_filter(array_keys($array), 'is_string')) > 0; } /** * get the delta between existing permissions and new permissions * * @param array $currentPrivileges * @param array $privilegesToSet associative array $user_id => $permissions * * @return array */ public function getDeltaPermissions(array $currentPrivileges, array $privilegesToSet): array { foreach ($privilegesToSet as $userId => $privilegeIds) { //if privileges are in request but not in db we add then if (!isset($currentPrivileges[$userId])) { if ($privilegeIds) { $add[$userId] = $privilegeIds; } } // compare privileges in db and request else { $tmp = array_values(array_diff($privilegeIds, $currentPrivileges[$userId])); if ($tmp) { $add[$userId] = $tmp; } $tmp = array_values(array_diff($currentPrivileges[$userId], $privilegeIds)); if ($tmp) { $remove[$userId] = $tmp; } // unset already compare db variable unset($currentPrivileges[$userId]); } } //remaining privileges has to be removed foreach ($currentPrivileges as $userId => $privilegeIds) { $remove[$userId] = $privilegeIds; } return compact('remove', 'add'); } }