1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace N98\Util; |
7
|
|
|
|
8
|
|
|
class ArrayFunctions |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Merge two arrays together. |
12
|
|
|
* |
13
|
|
|
* If an integer key exists in both arrays, the value from the second array |
14
|
|
|
* will be appended the the first array. If both values are arrays, they |
15
|
|
|
* are merged together, else the value of the second array overwrites the |
16
|
|
|
* one of the first array. |
17
|
|
|
* |
18
|
|
|
* @see http://packages.zendframework.com/docs/latest/manual/en/index.html#zend-stdlib |
19
|
|
|
* @param array $a |
20
|
|
|
* @param array $b |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
public static function mergeArrays(array $a, array $b) |
24
|
|
|
{ |
25
|
|
|
foreach ($b as $key => $value) { |
26
|
|
|
if (array_key_exists($key, $a)) { |
27
|
|
|
if (is_int($key)) { |
28
|
|
|
$a[] = $value; |
29
|
|
|
} elseif (is_array($value) && is_array($a[$key])) { |
30
|
|
|
$a[$key] = self::mergeArrays($a[$key], $value); |
31
|
|
|
} else { |
32
|
|
|
$a[$key] = $value; |
33
|
|
|
} |
34
|
|
|
} else { |
35
|
|
|
$a[$key] = $value; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $a; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string[] $columns |
44
|
|
|
* @param array $table |
45
|
|
|
* @return array table with ordered columns |
46
|
|
|
*/ |
47
|
|
|
public static function columnOrderArrayTable(array $columns, array $table) |
48
|
|
|
{ |
49
|
|
|
return array_map(function (array $array) use ($columns) { |
50
|
|
|
return self::columnOrder($columns, $array); |
51
|
|
|
}, $table); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* order array entries (named and numbered) of array by the columns given as string keys. |
56
|
|
|
* |
57
|
|
|
* non-existent columns default to numbered entries or if no numbered entries exists any longer, to null. |
58
|
|
|
* |
59
|
|
|
* entries in array that could not consume any column are put after the columns. |
60
|
|
|
* |
61
|
|
|
* @param string[] $columns |
62
|
|
|
* @param array $array |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public static function columnOrder(array $columns, array $array) |
66
|
|
|
{ |
67
|
|
|
if (!$columns) { |
68
|
|
|
return $array; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$keys = array_fill_keys($columns, null); |
72
|
|
|
|
73
|
|
|
$keyed = array_intersect_key($array, $keys); |
74
|
|
|
|
75
|
|
|
$arrayLeftover = array_diff_key($array, $keyed); |
76
|
|
|
$keysLeftover = array_diff_key($keys, $keyed); |
77
|
|
|
|
78
|
|
|
$target = array(); |
79
|
|
|
if ($keysLeftover) { |
80
|
|
|
foreach ($arrayLeftover as $key => $value) { |
81
|
|
|
if (is_string($key)) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
$target[key($keysLeftover)] = $value; |
85
|
|
|
unset($arrayLeftover[$key]); |
86
|
|
|
next($keysLeftover); |
87
|
|
|
if (null === key($keysLeftover)) { |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$result = array_merge($keys, $keyed, $keysLeftover, $target, $arrayLeftover); |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|