TYPO3 CMS  TYPO3_7-6
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  */
16 
23 {
33  public static function integerExplode($delimiter, $string)
34  {
35  $explodedValues = explode($delimiter, $string);
36  foreach ($explodedValues as &$value) {
37  $value = (int)$value;
38  }
39  unset($value);
40  return $explodedValues;
41  }
42 
53  public static function trimExplode($delimiter, $string, $onlyNonEmptyValues = false)
54  {
55  $chunksArr = explode($delimiter, $string);
56  $newChunksArr = [];
57  foreach ($chunksArr as $value) {
58  if ($onlyNonEmptyValues === false || trim($value) !== '') {
59  $newChunksArr[] = trim($value);
60  }
61  }
62  reset($newChunksArr);
63  return $newChunksArr;
64  }
65 
77  public static function arrayMergeRecursiveOverrule(array $firstArray, array $secondArray, $dontAddNewKeys = false, $emptyValuesOverride = true)
78  {
79  foreach ($secondArray as $key => $value) {
80  if (array_key_exists($key, $firstArray) && is_array($firstArray[$key])) {
81  if (is_array($secondArray[$key])) {
82  $firstArray[$key] = self::arrayMergeRecursiveOverrule($firstArray[$key], $secondArray[$key], $dontAddNewKeys, $emptyValuesOverride);
83  } else {
84  $firstArray[$key] = $secondArray[$key];
85  }
86  } else {
87  if ($dontAddNewKeys) {
88  if (array_key_exists($key, $firstArray)) {
89  if ($emptyValuesOverride || !empty($value)) {
90  $firstArray[$key] = $value;
91  }
92  }
93  } else {
94  if ($emptyValuesOverride || !empty($value)) {
95  $firstArray[$key] = $value;
96  }
97  }
98  }
99  }
100  reset($firstArray);
101  return $firstArray;
102  }
103 
112  public static function randomizeArrayOrder(array $array)
113  {
114  $reorderedArray = [];
115  if (count($array) > 1) {
116  $keysInRandomOrder = array_rand($array, count($array));
117  foreach ($keysInRandomOrder as $key) {
118  $reorderedArray[] = $array[$key];
119  }
120  } else {
121  $reorderedArray = $array;
122  }
123  return $reorderedArray;
124  }
125 
133  public static function containsMultipleTypes(array $array)
134  {
135  if (!empty($array)) {
136  foreach ($array as $value) {
137  if (!isset($previousType)) {
138  $previousType = gettype($value);
139  } elseif ($previousType !== gettype($value)) {
140  return true;
141  }
142  }
143  }
144  return false;
145  }
146 
157  public static function array_reduce(array $array, $function, $initial = null)
158  {
159  $accumlator = $initial;
160  foreach ($array as $value) {
161  $accumlator = $function($accumlator, $value);
162  }
163  return $accumlator;
164  }
165 
175  public static function getValueByPath(array &$array, $path)
176  {
177  if (is_string($path)) {
178  $path = explode('.', $path);
179  } elseif (!is_array($path)) {
180  throw new \InvalidArgumentException('getValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1304950007);
181  }
182  $key = array_shift($path);
183  if (isset($array[$key])) {
184  if (!empty($path)) {
185  return is_array($array[$key]) ? self::getValueByPath($array[$key], $path) : null;
186  }
187  return $array[$key];
188  } else {
189  return null;
190  }
191  }
192 
202  public static function setValueByPath($subject, $path, $value)
203  {
204  if (!is_array($subject) && !$subject instanceof \ArrayAccess) {
205  throw new \InvalidArgumentException('setValueByPath() expects $subject to be array or an object implementing \\ArrayAccess, "' . (is_object($subject) ? get_class($subject) : gettype($subject)) . '" given.', 1306424308);
206  }
207  if (is_string($path)) {
208  $path = explode('.', $path);
209  } elseif (!is_array($path)) {
210  throw new \InvalidArgumentException('setValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1305111499);
211  }
212  $key = array_shift($path);
213  if (empty($path)) {
214  $subject[$key] = $value;
215  } else {
216  if (!isset($subject[$key]) || !is_array($subject[$key])) {
217  $subject[$key] = [];
218  }
219  $subject[$key] = self::setValueByPath($subject[$key], $path, $value);
220  }
221  return $subject;
222  }
223 
232  public static function unsetValueByPath(array $array, $path)
233  {
234  if (is_string($path)) {
235  $path = explode('.', $path);
236  } elseif (!is_array($path)) {
237  throw new \InvalidArgumentException('unsetValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1305111513);
238  }
239  $key = array_shift($path);
240  if (empty($path)) {
241  unset($array[$key]);
242  } else {
243  if (!isset($array[$key]) || !is_array($array[$key])) {
244  return $array;
245  }
246  $array[$key] = self::unsetValueByPath($array[$key], $path);
247  }
248  return $array;
249  }
250 
260  public static function sortKeysRecursively(array &$array, $sortFlags = null)
261  {
262  foreach ($array as &$value) {
263  if (is_array($value)) {
264  if (self::sortKeysRecursively($value, $sortFlags) === false) {
265  return false;
266  }
267  }
268  }
269  return ksort($array, $sortFlags);
270  }
271 
279  public static function convertObjectToArray($subject)
280  {
281  if (!is_object($subject) && !is_array($subject)) {
282  throw new \InvalidArgumentException('convertObjectToArray expects either array or object as input, ' . gettype($subject) . ' given.', 1287059709);
283  }
284  if (is_object($subject)) {
285  $subject = (array)$subject;
286  }
287  foreach ($subject as $key => $value) {
288  if (is_array($value) || is_object($value)) {
289  $subject[$key] = self::convertObjectToArray($value);
290  }
291  }
292  return $subject;
293  }
294 
301  public static function removeEmptyElementsRecursively(array $array)
302  {
303  $result = $array;
304  foreach ($result as $key => $value) {
305  if (is_array($value)) {
306  $result[$key] = self::removeEmptyElementsRecursively($value);
307  if ($result[$key] === []) {
308  unset($result[$key]);
309  }
310  } elseif ($value === null) {
311  unset($result[$key]);
312  }
313  }
314  return $result;
315  }
316 
324  public static function sortArrayWithIntegerKeys($array)
325  {
326  $containsNumericalKeysOnly = true;
327  array_walk($array, function ($value, $key) use (&$containsNumericalKeysOnly) {
328  if (!is_int($key)) {
329  $containsNumericalKeysOnly = false;
330  return;
331  }
332  });
333  if ($containsNumericalKeysOnly === true) {
334  ksort($array);
335  }
336  return $array;
337  }
338 }
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)