TYPO3 CMS  TYPO3_8-7
ArrayUtility.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
17 
27 {
38  public static function integerExplode($delimiter, $string)
39  {
41  $explodedValues = explode($delimiter, $string);
42  foreach ($explodedValues as &$value) {
43  $value = (int)$value;
44  }
45  unset($value);
46  return $explodedValues;
47  }
48 
60  public static function trimExplode($delimiter, $string, $onlyNonEmptyValues = false)
61  {
63  $chunksArr = explode($delimiter, $string);
64  $newChunksArr = [];
65  foreach ($chunksArr as $value) {
66  if ($onlyNonEmptyValues === false || trim($value) !== '') {
67  $newChunksArr[] = trim($value);
68  }
69  }
70  reset($newChunksArr);
71  return $newChunksArr;
72  }
73 
86  public static function arrayMergeRecursiveOverrule(array $firstArray, array $secondArray, $dontAddNewKeys = false, $emptyValuesOverride = true)
87  {
89  foreach ($secondArray as $key => $value) {
90  if (array_key_exists($key, $firstArray) && is_array($firstArray[$key])) {
91  if (is_array($secondArray[$key])) {
92  $firstArray[$key] = self::arrayMergeRecursiveOverrule($firstArray[$key], $secondArray[$key], $dontAddNewKeys, $emptyValuesOverride);
93  } else {
94  $firstArray[$key] = $secondArray[$key];
95  }
96  } else {
97  if ($dontAddNewKeys) {
98  if (array_key_exists($key, $firstArray)) {
99  if ($emptyValuesOverride || !empty($value)) {
100  $firstArray[$key] = $value;
101  }
102  }
103  } else {
104  if ($emptyValuesOverride || !empty($value)) {
105  $firstArray[$key] = $value;
106  }
107  }
108  }
109  }
110  reset($firstArray);
111  return $firstArray;
112  }
113 
123  public static function randomizeArrayOrder(array $array)
124  {
126  $reorderedArray = [];
127  if (count($array) > 1) {
128  $keysInRandomOrder = array_rand($array, count($array));
129  foreach ($keysInRandomOrder as $key) {
130  $reorderedArray[] = $array[$key];
131  }
132  } else {
133  $reorderedArray = $array;
134  }
135  return $reorderedArray;
136  }
137 
146  public static function containsMultipleTypes(array $array)
147  {
149  if (!empty($array)) {
150  foreach ($array as $value) {
151  if (!isset($previousType)) {
152  $previousType = gettype($value);
153  } elseif ($previousType !== gettype($value)) {
154  return true;
155  }
156  }
157  }
158  return false;
159  }
160 
172  public static function array_reduce(array $array, $function, $initial = null)
173  {
175  $accumlator = $initial;
176  foreach ($array as $value) {
177  $accumlator = $function($accumlator, $value);
178  }
179  return $accumlator;
180  }
181 
192  public static function getValueByPath(array &$array, $path)
193  {
195  if (is_string($path)) {
196  $path = explode('.', $path);
197  } elseif (!is_array($path)) {
198  throw new \InvalidArgumentException('getValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1304950007);
199  }
200  $key = array_shift($path);
201  if (isset($array[$key])) {
202  if (!empty($path)) {
203  return is_array($array[$key]) ? self::getValueByPath($array[$key], $path) : null;
204  }
205  return $array[$key];
206  }
207  return null;
208  }
209 
220  public static function setValueByPath($subject, $path, $value)
221  {
223  if (!is_array($subject) && !$subject instanceof \ArrayAccess) {
224  throw new \InvalidArgumentException('setValueByPath() expects $subject to be array or an object implementing \\ArrayAccess, "' . (is_object($subject) ? get_class($subject) : gettype($subject)) . '" given.', 1306424308);
225  }
226  if (is_string($path)) {
227  $path = explode('.', $path);
228  } elseif (!is_array($path)) {
229  throw new \InvalidArgumentException('setValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1305111499);
230  }
231  $key = array_shift($path);
232  if (empty($path)) {
233  $subject[$key] = $value;
234  } else {
235  if (!isset($subject[$key]) || !is_array($subject[$key])) {
236  $subject[$key] = [];
237  }
238  $subject[$key] = self::setValueByPath($subject[$key], $path, $value);
239  }
240  return $subject;
241  }
242 
252  public static function unsetValueByPath(array $array, $path)
253  {
255  if (is_string($path)) {
256  $path = explode('.', $path);
257  } elseif (!is_array($path)) {
258  throw new \InvalidArgumentException('unsetValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1305111513);
259  }
260  $key = array_shift($path);
261  if (empty($path)) {
262  unset($array[$key]);
263  } else {
264  if (!isset($array[$key]) || !is_array($array[$key])) {
265  return $array;
266  }
267  $array[$key] = self::unsetValueByPath($array[$key], $path);
268  }
269  return $array;
270  }
271 
282  public static function sortKeysRecursively(array &$array, $sortFlags = null)
283  {
285  foreach ($array as &$value) {
286  if (is_array($value)) {
287  if (self::sortKeysRecursively($value, $sortFlags) === false) {
288  return false;
289  }
290  }
291  }
292  return ksort($array, $sortFlags);
293  }
294 
303  public static function convertObjectToArray($subject)
304  {
306  if (!is_object($subject) && !is_array($subject)) {
307  throw new \InvalidArgumentException('convertObjectToArray expects either array or object as input, ' . gettype($subject) . ' given.', 1287059709);
308  }
309  if (is_object($subject)) {
310  $subject = (array)$subject;
311  }
312  foreach ($subject as $key => $value) {
313  if (is_array($value) || is_object($value)) {
314  $subject[$key] = self::convertObjectToArray($value);
315  }
316  }
317  return $subject;
318  }
319 
327  public static function removeEmptyElementsRecursively(array $array)
328  {
330  $result = $array;
331  foreach ($result as $key => $value) {
332  if (is_array($value)) {
333  $result[$key] = self::removeEmptyElementsRecursively($value);
334  if ($result[$key] === []) {
335  unset($result[$key]);
336  }
337  } elseif ($value === null) {
338  unset($result[$key]);
339  }
340  }
341  return $result;
342  }
343 
352  public static function sortArrayWithIntegerKeys($array)
353  {
355  $containsNumericalKeysOnly = true;
356  array_walk($array, function ($value, $key) use (&$containsNumericalKeysOnly) {
357  if (!is_int($key)) {
358  $containsNumericalKeysOnly = false;
359  return;
360  }
361  });
362  if ($containsNumericalKeysOnly === true) {
363  ksort($array);
364  }
365  return $array;
366  }
367 }
static array_reduce(array $array, $function, $initial=null)
static removeEmptyElementsRecursively(array $array)
static sortKeysRecursively(array &$array, $sortFlags=null)
static randomizeArrayOrder(array $array)
static trimExplode($delimiter, $string, $onlyNonEmptyValues=false)
static integerExplode($delimiter, $string)
static containsMultipleTypes(array $array)
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)