TYPO3 CMS  TYPO3_6-2
ArrayUtility.php
Go to the documentation of this file.
1 <?php
3 
22 class ArrayUtility {
23 
33  static public function integerExplode($delimiter, $string) {
34  $explodedValues = explode($delimiter, $string);
35  foreach ($explodedValues as &$value) {
36  $value = (int)$value;
37  }
38  unset($value);
39  return $explodedValues;
40  }
41 
52  static public function trimExplode($delimiter, $string, $onlyNonEmptyValues = FALSE) {
53  $chunksArr = explode($delimiter, $string);
54  $newChunksArr = array();
55  foreach ($chunksArr as $value) {
56  if ($onlyNonEmptyValues === FALSE || trim($value) !== '') {
57  $newChunksArr[] = trim($value);
58  }
59  }
60  reset($newChunksArr);
61  return $newChunksArr;
62  }
63 
75  static public function arrayMergeRecursiveOverrule(array $firstArray, array $secondArray, $dontAddNewKeys = FALSE, $emptyValuesOverride = TRUE) {
76  foreach ($secondArray as $key => $value) {
77  if (array_key_exists($key, $firstArray) && is_array($firstArray[$key])) {
78  if (is_array($secondArray[$key])) {
79  $firstArray[$key] = self::arrayMergeRecursiveOverrule($firstArray[$key], $secondArray[$key], $dontAddNewKeys, $emptyValuesOverride);
80  } else {
81  $firstArray[$key] = $secondArray[$key];
82  }
83  } else {
84  if ($dontAddNewKeys) {
85  if (array_key_exists($key, $firstArray)) {
86  if ($emptyValuesOverride || !empty($value)) {
87  $firstArray[$key] = $value;
88  }
89  }
90  } else {
91  if ($emptyValuesOverride || !empty($value)) {
92  $firstArray[$key] = $value;
93  }
94  }
95  }
96  }
97  reset($firstArray);
98  return $firstArray;
99  }
100 
109  static public function randomizeArrayOrder(array $array) {
110  $reorderedArray = array();
111  if (count($array) > 1) {
112  $keysInRandomOrder = array_rand($array, count($array));
113  foreach ($keysInRandomOrder as $key) {
114  $reorderedArray[] = $array[$key];
115  }
116  } else {
117  $reorderedArray = $array;
118  }
119  return $reorderedArray;
120  }
121 
129  static public function containsMultipleTypes(array $array) {
130  if (count($array) > 0) {
131  foreach ($array as $value) {
132  if (!isset($previousType)) {
133  $previousType = gettype($value);
134  } elseif ($previousType !== gettype($value)) {
135  return TRUE;
136  }
137  }
138  }
139  return FALSE;
140  }
141 
152  static public function array_reduce(array $array, $function, $initial = NULL) {
153  $accumlator = $initial;
154  foreach ($array as $value) {
155  $accumlator = $function($accumlator, $value);
156  }
157  return $accumlator;
158  }
159 
169  static public function getValueByPath(array &$array, $path) {
170  if (is_string($path)) {
171  $path = explode('.', $path);
172  } elseif (!is_array($path)) {
173  throw new \InvalidArgumentException('getValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1304950007);
174  }
175  $key = array_shift($path);
176  if (isset($array[$key])) {
177  if (count($path) > 0) {
178  return is_array($array[$key]) ? self::getValueByPath($array[$key], $path) : NULL;
179  } else {
180  return $array[$key];
181  }
182  } else {
183  return NULL;
184  }
185  }
186 
196  static public function setValueByPath($subject, $path, $value) {
197  if (!is_array($subject) && !$subject instanceof \ArrayAccess) {
198  throw new \InvalidArgumentException('setValueByPath() expects $subject to be array or an object implementing \\ArrayAccess, "' . (is_object($subject) ? get_class($subject) : gettype($subject)) . '" given.', 1306424308);
199  }
200  if (is_string($path)) {
201  $path = explode('.', $path);
202  } elseif (!is_array($path)) {
203  throw new \InvalidArgumentException('setValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1305111499);
204  }
205  $key = array_shift($path);
206  if (count($path) === 0) {
207  $subject[$key] = $value;
208  } else {
209  if (!isset($subject[$key]) || !is_array($subject[$key])) {
210  $subject[$key] = array();
211  }
212  $subject[$key] = self::setValueByPath($subject[$key], $path, $value);
213  }
214  return $subject;
215  }
216 
225  static public function unsetValueByPath(array $array, $path) {
226  if (is_string($path)) {
227  $path = explode('.', $path);
228  } elseif (!is_array($path)) {
229  throw new \InvalidArgumentException('unsetValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1305111513);
230  }
231  $key = array_shift($path);
232  if (count($path) === 0) {
233  unset($array[$key]);
234  } else {
235  if (!isset($array[$key]) || !is_array($array[$key])) {
236  return $array;
237  }
238  $array[$key] = self::unsetValueByPath($array[$key], $path);
239  }
240  return $array;
241  }
242 
252  static public function sortKeysRecursively(array &$array, $sortFlags = NULL) {
253  foreach ($array as &$value) {
254  if (is_array($value)) {
255  if (self::sortKeysRecursively($value, $sortFlags) === FALSE) {
256  return FALSE;
257  }
258  }
259  }
260  return ksort($array, $sortFlags);
261  }
262 
270  static public function convertObjectToArray($subject) {
271  if (!is_object($subject) && !is_array($subject)) {
272  throw new \InvalidArgumentException('convertObjectToArray expects either array or object as input, ' . gettype($subject) . ' given.', 1287059709);
273  }
274  if (is_object($subject)) {
275  $subject = (array) $subject;
276  }
277  foreach ($subject as $key => $value) {
278  if (is_array($value) || is_object($value)) {
279  $subject[$key] = self::convertObjectToArray($value);
280  }
281  }
282  return $subject;
283  }
284 
291  static public function removeEmptyElementsRecursively(array $array) {
292  $result = $array;
293  foreach ($result as $key => $value) {
294  if (is_array($value)) {
295  $result[$key] = self::removeEmptyElementsRecursively($value);
296  if ($result[$key] === array()) {
297  unset($result[$key]);
298  }
299  } elseif ($value === NULL) {
300  unset($result[$key]);
301  }
302  }
303  return $result;
304  }
305 
313  public static function sortArrayWithIntegerKeys($array) {
314  $containsNumericalKeysOnly = TRUE;
315  array_walk($array, function($value, $key) use (&$containsNumericalKeysOnly) {
316  if (!is_integer($key)) {
317  $containsNumericalKeysOnly = FALSE;
318  return;
319  }
320  });
321  if ($containsNumericalKeysOnly === TRUE) {
322  ksort($array);
323  }
324  return $array;
325  }
326 }
static sortKeysRecursively(array &$array, $sortFlags=NULL)
static removeEmptyElementsRecursively(array $array)
static randomizeArrayOrder(array $array)
static integerExplode($delimiter, $string)
static trimExplode($delimiter, $string, $onlyNonEmptyValues=FALSE)
static containsMultipleTypes(array $array)
static array_reduce(array $array, $function, $initial=NULL)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
static arrayMergeRecursiveOverrule(array $firstArray, array $secondArray, $dontAddNewKeys=FALSE, $emptyValuesOverride=TRUE)
static setValueByPath($subject, $path, $value)
static unsetValueByPath(array $array, $path)
static getValueByPath(array &$array, $path)