1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Helpers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Arrays |
7
|
|
|
* @package DrMVC\Helpers |
8
|
|
|
*/ |
9
|
|
|
class Arrays |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Order array by arguments |
13
|
|
|
* |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
public static function orderBy(): array |
17
|
|
|
{ |
18
|
|
|
$args = \func_get_args(); |
19
|
|
|
$data = array_shift($args); |
20
|
|
|
foreach ($args as $n => $field) { |
21
|
|
|
if (\is_string($field)) { |
22
|
|
|
$tmp = []; |
23
|
|
|
foreach ($data as $key => $row) { |
24
|
|
|
$tmp[$key] = $row[$field]; |
25
|
|
|
} |
26
|
|
|
$args[$n] = $tmp; |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
$args[] = &$data; |
30
|
|
|
array_multisort($args); |
31
|
|
|
return array_pop($args); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Check if array is multidimensional |
36
|
|
|
* |
37
|
|
|
* @param array $array |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public static function isMulti($array): bool |
41
|
|
|
{ |
42
|
|
|
// First we need know what value is array |
43
|
|
|
if (\is_array($array)) { |
|
|
|
|
44
|
|
|
// And if some element is array then $array is multidimensional |
45
|
|
|
foreach ($array as $value) { |
46
|
|
|
if (\is_array($value)) { |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if two arrays is equal, with same keys |
56
|
|
|
* |
57
|
|
|
* @param array $a |
58
|
|
|
* @param array $b |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public static function equal(array $a, array $b): bool |
62
|
|
|
{ |
63
|
|
|
// Both arrays should be... arrays |
64
|
|
|
$is_arrays = (\is_array($a) && \is_array($b)); |
|
|
|
|
65
|
|
|
// Elements count should be equal |
66
|
|
|
$count_equal = (\count($a) === \count($b)); |
67
|
|
|
// Between the keys should not be differences |
68
|
|
|
$difference = (array_diff($a, $b) === array_diff($b, $a)); |
69
|
|
|
|
70
|
|
|
return ($is_arrays && $count_equal && $difference); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Read any files and folder into some directory |
75
|
|
|
* |
76
|
|
|
* @param string $path - Source directory with files |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public static function dirToArr($path): array |
80
|
|
|
{ |
81
|
|
|
$result = []; |
82
|
|
|
$dirContent = scandir($path, null); |
83
|
|
|
|
84
|
|
|
foreach ($dirContent as $key => $value) { |
85
|
|
|
// Exclude system dots folders |
86
|
|
|
if (!\in_array($value, ['.', '..'])) { |
87
|
|
|
if (is_dir($path . DIRECTORY_SEPARATOR . $value)) { |
88
|
|
|
$result[$value] = self::dirToArr($path . DIRECTORY_SEPARATOR . $value); |
89
|
|
|
} else { |
90
|
|
|
$result[] = $value; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Find nested array inside two-dimensional array |
100
|
|
|
* |
101
|
|
|
* @param array|object $multidimensional - Where need to find |
102
|
|
|
* @param array|object $target - What we need to find |
103
|
|
|
* @param bool $isObject - Method work mode, if object then true, if array then false |
104
|
|
|
* @return bool|array |
105
|
|
|
*/ |
106
|
|
|
public static function searchMd($multidimensional, $target, $isObject = true) |
107
|
|
|
{ |
108
|
|
|
if (empty($target) || empty($multidimensional)) { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$output = array_map( |
113
|
|
|
function($element) use ($target, $isObject) { |
114
|
|
|
$exist = true; |
115
|
|
|
foreach ($target as $skey => $svalue) { |
116
|
|
|
$exist = $isObject |
117
|
|
|
? ($exist && isset($element->$skey) && ($element->$skey === $svalue)) |
118
|
|
|
: ($exist && isset($element[$skey]) && ($element[$skey] === $svalue)); |
119
|
|
|
} |
120
|
|
|
return $exist ? $element : null; |
121
|
|
|
}, |
122
|
|
|
$multidimensional |
|
|
|
|
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
// If output is not empty, false by default |
126
|
|
|
return !empty($output) ? array_filter($output) : false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|