-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathArrayTool.php
More file actions
68 lines (63 loc) · 1.68 KB
/
ArrayTool.php
File metadata and controls
68 lines (63 loc) · 1.68 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
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
namespace PHPJava\Utilities;
class ArrayTool
{
public static function concat(&$basedArray, ...$elements): void
{
if (empty($elements)) {
return;
}
array_push(
$basedArray,
...$elements
);
}
public static function stringify(array $array): string
{
return implode(
array_map(
static function ($value) {
if (is_object($value)) {
return spl_object_hash($value);
}
if (is_array($value)) {
return static::stringify($value);
}
return $value;
},
$array
)
);
}
public static function compare(array $array1, array $array2): bool
{
return static::stringify($array1) === static::stringify($array2);
}
public static function containInMultipleDimension(array $array, $targetKey, $value): ?array
{
foreach ($array as $element) {
if (!is_array($element)) {
return false;
}
if (array_key_exists($targetKey, $element)
&& $element[$targetKey] === $value
) {
return $element;
}
}
return null;
}
public static function deepCopy(array $array): array
{
array_walk_recursive(
$array,
static function (&$element) {
if (is_object($element)) {
$element = clone $element;
}
}
);
return $array;
}
}