-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathHelper.php
More file actions
58 lines (55 loc) · 1.99 KB
/
Helper.php
File metadata and controls
58 lines (55 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/**
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
* @license https://github.com/cebe/php-openapi/blob/master/LICENSE
*/
namespace cebe\openapi;
/**
* Helper class containing widely used custom functions used in library
*/
class Helper
{
/**
* Thanks https://www.php.net/manual/en/function.array-merge-recursive.php#96201
*
* Merges any number of arrays / parameters recursively, replacing
* entries with string keys with values from latter arrays.
* If the entry or the next value to be assigned is an array, then it
* automagically treats both arguments as an array.
* Numeric entries are appended, not replaced, but only if they are
* unique
*
* Function call example: `$result = array_merge_recursive_distinct(a1, a2, ... aN);`
* More documentation is present at [array merge recursive distinct.md](../../../doc/array merge recursive distinct.md) file
* @return array
*/
public static function arrayMergeRecursiveDistinct()
{
$arrays = func_get_args();
$base = array_shift($arrays);
if (!is_array($base)) {
$base = empty($base) ? [] : [$base];
}
foreach ($arrays as $append) {
if (!is_array($append)) {
$append = [$append];
}
foreach ($append as $key => $value) {
if (!array_key_exists($key, $base) and !is_numeric($key)) {
$base[$key] = $append[$key];
continue;
}
if (is_array($value) || is_array($base[$key])) {
$base[$key] = static::arrayMergeRecursiveDistinct($base[$key], $append[$key]);
} elseif (is_numeric($key)) {
if (!in_array($value, $base)) {
$base[] = $value;
}
} else {
$base[$key] = $value;
}
}
}
return $base;
}
}