2 namespace TYPO3\CMS\Core\Utility;
57 $resultArray = array();
61 $callback =
function (&$value, $key) use ($needle, &$resultArray) {
62 if ($value === $needle) {
63 ($resultArray[$key] = $value);
64 }
elseif (is_array($value)) {
65 ($subArrayMatches = static::filterByValueRecursive($needle, $value));
66 if (!empty($subArrayMatches)) {
67 ($resultArray[$key] = $subArrayMatches);
72 array_walk($haystack, $callback);
95 public static function isValidPath(array $array, $path, $delimiter =
'/')
100 static::getValueByPath($array, $path, $delimiter);
101 }
catch (\RuntimeException $e) {
134 throw new \RuntimeException(
'Path must not be empty', 1341397767);
137 $path = str_getcsv($path, $delimiter);
140 foreach ($path as $segment) {
141 if (array_key_exists($segment, $value)) {
143 $value = $value[$segment];
146 throw new \RuntimeException(
'Path does not exist in array', 1341397869);
178 public static function setValueByPath(array $array, $path, $value, $delimiter =
'/')
181 throw new \RuntimeException(
'Path must not be empty', 1341406194);
183 if (!is_string($path)) {
184 throw new \RuntimeException(
'Path must be a string', 1341406402);
187 $path = str_getcsv($path, $delimiter);
191 foreach ($path as $segment) {
193 if (empty($segment)) {
194 throw new \RuntimeException(
'Invalid path segment specified', 1341406846);
197 if (!array_key_exists($segment, $pointer)) {
198 $pointer[$segment] = array();
201 $pointer = &$pointer[$segment];
217 public static function removeByPath(array $array, $path, $delimiter =
'/')
220 throw new \RuntimeException(
'Path must not be empty', 1371757718);
222 if (!is_string($path)) {
223 throw new \RuntimeException(
'Path must be a string', 1371757719);
226 $path = str_getcsv($path, $delimiter);
227 $pathDepth = count($path);
231 foreach ($path as $segment) {
234 if (empty($segment)) {
235 throw new \RuntimeException(
'Invalid path segment specified', 1371757720);
237 if (!array_key_exists($segment, $pointer)) {
238 throw new \RuntimeException(
'Path segment ' . $segment .
' does not exist in array', 1371758436);
240 if ($currentDepth === $pathDepth) {
241 unset($pointer[$segment]);
243 $pointer = &$pointer[$segment];
258 foreach ($array as $key => $value) {
259 if (is_array($value) && !empty($value)) {
260 $array[$key] = self::sortByKeyRecursive($value);
277 if (empty($arrays)) {
280 $sortResult = uasort($arrays,
function (array $a, array $b) use ($key, $ascending) {
281 if (!isset($a[$key]) || !isset($b[$key])) {
282 throw new \RuntimeException(
'The specified sorting key "' . $key .
'" is not available in the given array.', 1373727309);
284 return ($ascending) ? strcasecmp($a[$key], $b[$key]) : strcasecmp($b[$key], $a[$key]);
287 throw new \RuntimeException(
'The function uasort() failed for unknown reasons.', 1373727329);
303 public static function arrayExport(array $array = array(), $level = 0)
307 $writeKeyIndex =
false;
308 $expectedKeyIndex = 0;
309 foreach ($array as $key => $value) {
310 if ($key === $expectedKeyIndex) {
314 $writeKeyIndex =
true;
318 foreach ($array as $key => $value) {
320 $lines .= str_repeat(
' ', $level);
321 if ($writeKeyIndex) {
323 $lines .= is_int($key) ? $key .
' => ' :
'\'' . $key .
'\' =>
';
325 if (is_array($value)) {
326 if (!empty($value)) {
327 $lines .= self::arrayExport($value, $level);
329 $lines .= '[],
' . LF;
331 } elseif (is_int($value) || is_float($value)) {
332 $lines .= $value . ',
' . LF;
333 } elseif (is_null($value)) {
334 $lines .= 'null
' . ',
' . LF;
335 } elseif (is_bool($value)) {
336 $lines .= $value ? 'true' : 'false';
338 } elseif (is_string($value)) {
340 $stringContent = str_replace('\\
', '\\\\
', $value);
342 $stringContent = str_replace('\
'',
'\\\'', $stringContent);
343 $lines .=
'\'' . $stringContent .
'\'' .
',' . LF;
345 throw new \RuntimeException(
'Objects are not supported', 1342294987);
348 $lines .= str_repeat(
' ', ($level - 1)) .
']' . ($level - 1 == 0 ?
'' :
',' . LF);
385 public static function flatten(array $array, $prefix =
'')
387 $flatArray = array();
388 foreach ($array as $key => $value) {
390 $key = rtrim($key,
'.');
391 if (!is_array($value)) {
392 $flatArray[$prefix . $key] = $value;
394 $flatArray = array_merge($flatArray, self::flatten($value, $prefix . $key .
'.'));
437 $intersection = array();
438 foreach ($source as $key => $_) {
439 if (!array_key_exists($key, $mask)) {
442 if (is_array($source[$key]) && is_array($mask[$key])) {
443 $value = self::intersectRecursive($source[$key], $mask[$key]);
444 if (!empty($value)) {
445 $intersection[$key] = $value;
448 $intersection[$key] = $source[$key];
451 return $intersection;
482 $allKeysAreNumeric =
true;
483 foreach ($array as $key => $_) {
484 if (is_numeric($key) ===
false) {
485 $allKeysAreNumeric =
false;
489 $renumberedArray = $array;
490 if ($allKeysAreNumeric ===
true) {
491 $renumberedArray = array_values($array);
493 foreach ($renumberedArray as $key => $value) {
494 if (is_array($value)) {
495 $renumberedArray[$key] = self::renumberKeysToAvoidLeapsIfKeysAreAllNumeric($value, $level);
498 return $renumberedArray;
522 public static function mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys =
true, $includeEmptyValues =
true, $enableUnsetFeature =
true)
524 foreach ($overrule as $key => $_) {
525 if ($enableUnsetFeature && $overrule[$key] ===
'__UNSET') {
526 unset($original[$key]);
529 if (isset($original[$key]) && is_array($original[$key])) {
530 if (is_array($overrule[$key])) {
531 self::mergeRecursiveWithOverrule($original[$key], $overrule[$key], $addKeys, $includeEmptyValues, $enableUnsetFeature);
534 ($addKeys || isset($original[$key])) &&
535 ($includeEmptyValues || $overrule[$key])
537 $original[$key] = $overrule[$key];
566 public static function inArray(array $in_array, $item)
568 foreach ($in_array as $val) {
569 if (!is_array($val) && (
string)$val === (
string)$item) {
585 foreach ($array as $k => $v) {
587 $array[$k] = self::removeArrayEntryByValue($v, $cmpValue);
588 }
elseif ((
string)$v === (
string)$cmpValue) {
622 if (is_string($keepItems)) {
626 if (!is_callable($getValueFunc)) {
627 $getValueFunc = null;
630 if (is_array($keepItems) && !empty($keepItems)) {
631 foreach ($array as $key => $value) {
633 $keepValue = isset($getValueFunc) ? call_user_func($getValueFunc, $value) : $value;
634 if (!in_array($keepValue, $keepItems)) {
651 foreach ($mappingTable as $old => $new) {
652 if ($new && isset($array[$old])) {
653 $array[$new] = $array[$old];
669 $differenceArray = array();
670 foreach ($array1 as $key => $value) {
671 if (!array_key_exists($key, $array2)) {
672 $differenceArray[$key] = $value;
673 }
elseif (is_array($value)) {
674 if (is_array($array2[$key])) {
675 $differenceArray[$key] = self::arrayDiffAssocRecursive($value, $array2[$key]);
679 return $differenceArray;
690 uksort($array,
'strnatcasecmp');
691 foreach ($array as $key => &$value) {
692 if (is_array($value)) {
693 self::naturalKeySortRecursive($value);