-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.php
More file actions
216 lines (187 loc) · 5.49 KB
/
Array.php
File metadata and controls
216 lines (187 loc) · 5.49 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php // vim:ts=3:sts=3:sw=3:et:
/**
* Collection of array functions
*
* PHP Version 5
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @copyright 2005-2011 Bryan C. Geraghty
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
/**
* Collection of array functions
*
* @category PHP
* @package MindFrame2
* @author Bryan C. Geraghty <bryan@ravensight.org>
* @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL
* @link https://github.com/archwisp/MindFrame2
*/
class MindFrame2_Array
{
/**
* Extracts the specified column from a multi-dimensional array.
*
* @param array $input Input array
* @param mixed $key Array key of the column to be extracted
*
* @return array
*/
public static function extractColumn(array $input, $key)
{
$output = array();
foreach ($input as $record)
{
$output = $record[$key];
}
return $output;
}
public static function implodeUrlParameters($first_delimiter, array $parameters)
{
$query_string = NULL;
$delimiter = $first_delimiter;
foreach ($parameters as $key => $value)
{
$query_string .= sprintf('%s%s=%s', $delimiter, $key, $value);
$delimiter = '&';
}
return $query_string;
}
/**
* Converts an indexed array into an associative array based on a
* delimiter
*
* rekey(array('foo=bar'), '=');
*
* returns: array('foo' => 'bar')
*
* @param array $array The array be re-indexed
* @param string $delimiter The delimiter to split the element on
*
* @return array
*/
public static function rekey(array $array, $delimiter)
{
$new_array = array();
foreach ($array as $element)
{
$parts = explode($delimiter, $element);
$index = trim($parts[0]);
$value = trim(self::ifsetValue($parts, 1));
$new_array[$index] = $value;
}
// end foreach // array //
return($new_array);
}
/**
* This function should be used to obtain vailues from an array index
* which may not be defined
*
* @param array &$array The array to check
* @param string $index The index of the value to check for
*
* @return mixed
*/
public static function ifsetValue(array &$array, $index)
{
if (isset($array[$index]))
{
return($array[$index]);
}
else
{
return(NULL);
}
}
/**
* Attempts to explode the specified element by the specified delimiter into
* multiple elements. If it is successful, the input array is expanded, and
* the new elements are inserted where the target elment was. If the target
* element does not exist or the delimiter is not found within the target
* element, the input array is returned.
*
* @param string $delimiter Delimiter by which the target element will be
* split
* @param array $input The input array
* @param int $index The target element
*
* @return array
*/
public static function explodeElement($delimiter, array $input, $index)
{
MindFrame2_Core::assertArgumentIsInt($index, 3, 'index');
if (!array_key_exists($index, $input))
{
return $input;
}
if (strpos($input[$index], $delimiter) !== FALSE)
{
$begin = array_slice($input, 0, $index);
$exploded = explode($delimiter, $input[$index]);
$end = ($index + 1 < count($input))
? array_slice($input, $index + 1) : array();
$output = array_merge($begin, $exploded, $end);
return $output;
}
return $input;
}
/**
* Attempts to isolate the first pattern group from the remainder of the
* string. If it is successful, the input array is expanded, and the new
* elements are inserted where the target elment was. If the target element
* does not exist or the delimiter is not found within the target element,
* the input array is returned.
*
* @param string $pattern Regular expression by which the target element
* will be split
* @param array $input The input array
* @param int $index The target element
*
* @return array
*/
public static function pregSanitizeElement($pattern, array $input, $index)
{
MindFrame2_Core::assertArgumentIsInt($index, 3, 'index');
if (!array_key_exists($index, $input))
{
return $input;
}
$matches = array();
if (!preg_match($pattern, $input[$index], $matches)
|| ($matches[0] === $matches[1]))
{
return $input;
}
$split_pos = strlen($matches[1]);
$exploded = array(
substr($input[$index], 0, $split_pos),
trim(substr($input[$index], $split_pos)));
$begin = array_slice($input, 0, $index);
$end = ($index + 1 < count($input))
? array_slice($input, $index + 1) : array();
$output = array_merge($begin, $exploded, $end);
return $output;
}
/**
* Removes empty elements from the specified array.
*
* @param array $&array Array to be filtered
*
* @return void
*/
public static function unsetEmptyElements(array &$array)
{
foreach ($array as $key => $element)
{
if (empty($element))
{
unset($array[$key]);
}
// end if // (empty($element)) //
}
// end foreach // ($array as $key => $element) //
}
}