|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/allejo/PhpPulse/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\DaPulse\Utilities; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* This class contains static utilities used for manipulating arrays. |
|
12
|
|
|
* |
|
13
|
|
|
* The original authors for the respective functions are listed in the documentation. |
|
14
|
|
|
* |
|
15
|
|
|
* @package allejo\DaPulse\Utilities |
|
16
|
|
|
* @since 0.1.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
class ArrayUtilities |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Search a two-dimensional array based on a specific key located in one of the inner arrays. |
|
22
|
|
|
* |
|
23
|
|
|
* In the following example array, this function could search each 'first_name' key in each of the inner arrays. |
|
24
|
|
|
* |
|
25
|
|
|
* ```php |
|
26
|
|
|
* $records = array( |
|
27
|
|
|
* array( |
|
28
|
|
|
* 'id' => 2135, |
|
29
|
|
|
* 'first_name' => 'John', |
|
30
|
|
|
* 'last_name' => 'Doe', |
|
31
|
|
|
* ), |
|
32
|
|
|
* array( |
|
33
|
|
|
* 'id' => 3245, |
|
34
|
|
|
* 'first_name' => 'Sally', |
|
35
|
|
|
* 'last_name' => 'Smith', |
|
36
|
|
|
* ) |
|
37
|
|
|
* ); |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $array The target two-dimensional array we will be searching |
|
41
|
|
|
* @param string $column The name of the key that each inner array will have whose value will be checked |
|
42
|
|
|
* @param string $search The value that will be searched for |
|
43
|
|
|
* |
|
44
|
|
|
* @since 0.1.0 |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed This function returns an int or string if the key is found. This function will return false if the |
|
47
|
|
|
* key was not found; be sure to use "===" for comparison. |
|
48
|
|
|
*/ |
|
49
|
43 |
|
public static function array_search_column ($array, $column, $search) |
|
50
|
|
|
{ |
|
51
|
43 |
|
if (function_exists('array_column')) |
|
52
|
|
|
{ |
|
53
|
43 |
|
return array_search($search, array_column($array, $column)); |
|
54
|
|
|
} |
|
55
|
|
|
else |
|
56
|
|
|
{ |
|
57
|
|
|
foreach ($array as $key => $val) |
|
58
|
|
|
{ |
|
59
|
|
|
if ($val[$column] === $search) |
|
60
|
|
|
{ |
|
61
|
|
|
return $key; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* array_merge_recursive does indeed merge arrays, but it converts values with duplicate |
|
71
|
|
|
* keys to arrays rather than overwriting the value in the first array with the duplicate |
|
72
|
|
|
* value in the second array, as array_merge does. I.e., with array_merge_recursive, |
|
73
|
|
|
* this happens (documented behavior): |
|
74
|
|
|
* |
|
75
|
|
|
* array_merge_recursive(array('key' => 'org value'), array('key' => 'new value')); |
|
76
|
|
|
* => array('key' => array('org value', 'new value')); |
|
77
|
|
|
* |
|
78
|
|
|
* array_merge_recursive_distinct does not change the datatypes of the values in the arrays. |
|
79
|
|
|
* Matching keys' values in the second array overwrite those in the first array, as is the |
|
80
|
|
|
* case with array_merge, i.e.: |
|
81
|
|
|
* |
|
82
|
|
|
* array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value')); |
|
83
|
|
|
* => array('key' => array('new value')); |
|
84
|
|
|
* |
|
85
|
|
|
* Parameters are passed by reference, though only for performance reasons. They're not |
|
86
|
|
|
* altered by this function. |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $array1 |
|
89
|
|
|
* @param array $array2 |
|
90
|
|
|
* |
|
91
|
|
|
* @return array |
|
92
|
|
|
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk> |
|
93
|
|
|
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com> |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function array_merge_recursive_distinct (array &$array1, array &$array2) |
|
96
|
|
|
{ |
|
97
|
|
|
$merged = $array1; |
|
98
|
|
|
|
|
99
|
|
|
foreach ($array2 as $key => &$value) |
|
100
|
|
|
{ |
|
101
|
|
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) |
|
102
|
|
|
{ |
|
103
|
|
|
$merged[$key] = self::array_merge_recursive_distinct($merged[$key], $value); |
|
104
|
|
|
} |
|
105
|
|
|
else |
|
106
|
|
|
{ |
|
107
|
|
|
$merged[$key] = $value; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $merged; |
|
112
|
|
|
} |
|
113
|
|
|
} |