‪TYPO3CMS  ‪main
TYPO3\CMS\Core\Utility\ArrayUtility Class Reference

Static Public Member Functions

static assertAllArrayKeysAreValid (array $arrayToTest, array $allowedArrayKeys)
 
static convertBooleanStringsToBooleanRecursive (array $array)
 
static array filterByValueRecursive (mixed $needle='', array $haystack=[])
 
static bool isValidPath (array $array, array|string $path, string $delimiter='/')
 
static getValueByPath (array $array, array|string $path, string $delimiter='/')
 
static reIndexNumericArrayKeysRecursive (array $array)
 
static removeNullValuesRecursive (array $array)
 
static array setValueByPath (array $array, string|array|\ArrayAccess $path, mixed $value, string $delimiter='/')
 
static array removeByPath (array $array, string $path, string $delimiter='/')
 
static array sortByKeyRecursive (array $array)
 
static array sortArraysByKey (array $arrays, string $key, bool $ascending=true)
 
static string arrayExport (array $array=[], int $level=0)
 
static flatten (array $array, string $prefix='', bool $keepDots=false)
 
static flattenPlain (array $array)
 
static bool isAssociative (array $array)
 
static replaceAndAppendScalarValuesRecursive (array $array1, array $array2)
 

Detailed Description

Class with helper functions for array handling

Definition at line 25 of file ArrayUtility.php.

Member Function Documentation

◆ arrayExport()

static string TYPO3\CMS\Core\Utility\ArrayUtility::arrayExport ( array  $array = [],
int  $level = 0 
)
static

Exports an array as string. Similar to var_export(), but representation follows the PSR-2 and TYPO3 core CGL.

See unit tests for detailed examples

Parameters
array$array‪Array to export
int$level‪Internal level used for recursion, do not set from outside!
Returns
‪string String representation of array
Exceptions

Definition at line 386 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\arrayExportReturnsFormattedMultidimensionalArray(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\arrayExportReturnsKeyIndexForNonConsecutiveCountedArrays(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\arrayExportReturnsNoKeyIndexForConsecutiveCountedArrays(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\arrayExportReturnsNumericArrayKeys(), and TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\arrayExportThrowsExceptionIfObjectShouldBeExported().

◆ assertAllArrayKeysAreValid()

◆ convertBooleanStringsToBooleanRecursive()

static TYPO3\CMS\Core\Utility\ArrayUtility::convertBooleanStringsToBooleanRecursive ( array  $array)
static

Recursively convert 'true' and 'false' strings to boolean values.

Definition at line 51 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\convertBooleanStringsToBooleanRecursiveExpectConverting(), and TYPO3\CMS\Form\Mvc\Configuration\YamlSource\load().

◆ filterByValueRecursive()

static array TYPO3\CMS\Core\Utility\ArrayUtility::filterByValueRecursive ( mixed  $needle = '',
array  $haystack = [] 
)
static

Reduce an array by a search value and keep the array structure.

Comparison is type strict:

  • ‪For a given needle of type string, integer, array or boolean, value and value type must match to occur in result array
  • ‪For a given object, an object within the array must be a reference to the same object to match (not just different instance of same class)

Example:

  • ‪Needle: 'findMe'
  • ‪Given array: array( 'foo' => 'noMatch', 'bar' => 'findMe', 'foobar => array( 'foo' => 'findMe', ), );
  • ‪Result: array( 'bar' => 'findMe', 'foobar' => array( 'foo' => findMe', ), );

See the unit tests for more examples and expected behaviour

Parameters
mixed$needle‪The value to search for
array$haystack‪The array in which to search
Returns
‪array $haystack array reduced matching $needle values

Definition at line 101 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Core\Utility\ExtensionManagementUtility\addTcaSelectItem(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\filterByValueRecursiveCorrectlyFiltersArray(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\filterByValueRecursiveDoesNotMatchDifferentInstancesOfSameClass(), and TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\filterByValueRecursiveMatchesReferencesToSameObject().

◆ flatten()

static TYPO3\CMS\Core\Utility\ArrayUtility::flatten ( array  $array,
string  $prefix = '',
bool  $keepDots = false 
)
static

Converts a multidimensional array to a flat representation.

Todo:

: The current implementation isn't a generic array flatten method, but tailored for TypoScript flattening

: It should be deprecated and removed and the required specialities should be put under the domain of TypoScript parsing

See unit tests for more details

Example:

  • ‪array: array( 'first.' => array( 'second' => 1 ) )
  • ‪result: array( 'first.second' => 1 )

Example:

  • ‪array: array( 'first' => array( 'second' => 1 ) )
  • ‪result: array( 'first.second' => 1 )
Parameters
array$array‪The (relative) array to be converted
string$prefix‪The (relative) prefix to be used (e.g. 'section.')
bool$keepDots

Definition at line 469 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\flattenCalculatesExpectedResult(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\flattenWithKeepDotsCalculatesExpectedResult(), TYPO3\CMS\Backend\View\BackendLayout\PageTsBackendLayoutDataProvider\generateBackendLayoutFromTsConfig(), TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getPostStructure(), and TYPO3\CMS\Adminpanel\Modules\Info\UserIntInformation\getUserIntInfo().

◆ flattenPlain()

static TYPO3\CMS\Core\Utility\ArrayUtility::flattenPlain ( array  $array)
static

◆ getValueByPath()

static TYPO3\CMS\Core\Utility\ArrayUtility::getValueByPath ( array  $array,
array|string  $path,
string  $delimiter = '/' 
)
static

Returns a value by given path

Example

  • ‪array: array( 'foo' => array( 'bar' => array( 'baz' => 42 ) ) );
  • ‪path: foo/bar/baz
  • ‪return: 42

If a path segments contains a delimiter character, the path segment must be enclosed by " (double quote), see unit tests for details

Parameters
array$array‪Input array
array | string$path‪Path within the array
string$delimiter‪Defined path delimiter, default /
Exceptions

Definition at line 176 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\IsCreatableFormElementExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\MultiValuePropertiesExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\MultiValuePropertiesExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\PropertyPathsExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\SelectOptionsExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\SelectOptionsExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\IsCreatablePropertyCollectionElementExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FlexformConfiguration\Processors\FinisherOptionGenerator\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\AddHmacDataConverter\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FinisherOptionsFlexFormOverridesConverter\__invoke(), TYPO3\CMS\Form\Domain\Finishers\FinisherVariableProvider\exists(), TYPO3\CMS\Form\Domain\Finishers\FinisherVariableProvider\get(), TYPO3\CMS\Core\Configuration\ExtensionConfiguration\get(), TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\getData(), TYPO3\CMS\Form\Domain\Runtime\FormState\getFormValue(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\PropertyPathsExtractor\getPropertyPaths(), TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider\DefaultFunctionsProvider\getTraverseArrayFunction(), TYPO3\CMS\Form\Mvc\Configuration\InheritancesResolverService\getValueByPath(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\getValueByPathAcceptsDifferentDelimiter(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\getValueByPathGetsCorrectValue(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\getValueByPathReturnsFirstIndexIfPathIsZero(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\getValueByPathReturnsFirstIndexIfPathSegmentIsZero(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\getValueByPathThrowsExceptionIfPathIsEmpty(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\getValueByPathThrowsExceptionIfPathNotExists(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\getValueByPathThrowsSpecificExceptionIfPathNotExists(), TYPO3\CMS\Backend\Controller\Wizard\AddController\mainAction(), TYPO3\CMS\Form\Domain\Runtime\FormRuntime\mapAndValidatePage(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FinisherOptionsFlexFormOverridesConverter\optionValueHasChanged(), TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher\parseOption(), TYPO3\CMS\Backend\Form\FormDataProvider\AbstractItemProvider\parseSiteConfiguration(), TYPO3\CMS\Backend\Controller\Wizard\EditController\processRequest(), TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager\removeIgnoredFlexFormSettingsIfEmpty(), TYPO3\CMS\Reactions\Reaction\CreateRecordReaction\replacePlaceHolders(), TYPO3\CMS\Form\Controller\FormEditorController\transformMultiValuePropertiesForFormEditor(), TYPO3\CMS\Form\Service\TranslationService\translateFinisherOption(), TYPO3\CMS\Form\Service\TranslationService\translateFormElementValue(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ElementBasedValidator\validateFormElementPropertyValueByHmacData(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CollectionBasedValidator\validatePropertyCollectionElementPropertyValueByHmacData(), TYPO3\CMS\Core\Site\SiteSettingsFactory\validateSettings(), and TYPO3\CMS\Extbase\Tests\Functional\Mvc\Controller\ActionControllerArgumentTest\validationErrorReturnsToForwardedPreviousAction().

◆ isAssociative()

static bool TYPO3\CMS\Core\Utility\ArrayUtility::isAssociative ( array  $array)
static

Check whether the array has non-integer keys. If there is at least one string key, $array will be regarded as an associative array.

Returns
‪bool True in case a string key was found.

Definition at line 960 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\isAssociativeCorrectlyFindsStringKeys().

◆ isValidPath()

static bool TYPO3\CMS\Core\Utility\ArrayUtility::isValidPath ( array  $array,
array|string  $path,
string  $delimiter = '/' 
)
static

Checks if a given path exists in array

Example:

  • ‪array: array( 'foo' => array( 'bar' = 'test', ) );
  • ‪path: 'foo/bar'
  • ‪return: TRUE
Parameters
array$array‪Given array
array | string$path‪Path to test within the array
string$delimiter‪Delimiter for path, default /
Returns
‪bool TRUE if path exists in array

Definition at line 141 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\IsCreatableFormElementExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\PropertyCollectionElement\IsCreatablePropertyCollectionElementExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FrameworkConfiguration\Extractors\FormElement\MultiValuePropertiesExtractor\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FinisherTranslationLanguageConverter\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatableFormElementPropertiesValidator\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CreatablePropertyCollectionElementPropertiesValidator\__invoke(), TYPO3\CMS\Core\Configuration\ExtensionConfiguration\get(), TYPO3\CMS\Form\Domain\Configuration\ConfigurationService\isFormElementTypeDefinedInFormSetup(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\isValidPathReturnsFalseIfPathDoesNotExistArrayVersion(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\isValidPathReturnsFalseIfPathDoesNotExistStringVersion(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\isValidPathReturnsTrueIfPathExistsArrayVersion(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\isValidPathReturnsTrueIfPathExistsStringVersion(), TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager\removeIgnoredFlexFormSettingsIfEmpty(), TYPO3\CMS\Form\Controller\FormEditorController\transformMultiValuePropertiesForFormEditor(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\ElementBasedValidator\validateFormElementPropertyValueByHmacData(), and TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Validators\CollectionBasedValidator\validatePropertyCollectionElementPropertyValueByHmacData().

◆ reIndexNumericArrayKeysRecursive()

static TYPO3\CMS\Core\Utility\ArrayUtility::reIndexNumericArrayKeysRecursive ( array  $array)
static

◆ removeByPath()

static array TYPO3\CMS\Core\Utility\ArrayUtility::removeByPath ( array  $array,
string  $path,
string  $delimiter = '/' 
)
static

Remove a sub part from an array specified by path

Parameters
array$array‪Input array to manipulate
string$path‪Path to remove from array
string$delimiter‪Path delimiter
Returns
‪array Modified array
Exceptions

Definition at line 303 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\RemoveHmacDataConverter\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FinisherTranslationLanguageConverter\__invoke(), TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\EvaluateDisplayConditionsTest\addDataRemovesTcaReferencingOtherFieldsInDisplayCondition(), TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getPostStructure(), TYPO3\CMS\Form\Domain\Finishers\FinisherVariableProvider\remove(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\removeByPathAcceptsGivenDelimiter(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\removeByPathRemovesCorrectPath(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\removeByPathRemovesFirstIndexWithZeroAsPath(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\removeByPathRemovesFirstIndexWithZeroAsPathSegment(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\removeByPathThrowsExceptionIfPathDoesNotExistInArray(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\removeByPathThrowsExceptionIfPathIsEmpty(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\removeByPathThrowsExceptionWithEmptyPathSegment(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\removeByPathThrowsSpecificExceptionIfPathDoesNotExistInArray(), TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager\removeIgnoredFlexFormSettingsIfEmpty(), and TYPO3\CMS\Core\Site\SiteSettingsFactory\validateSettings().

◆ removeNullValuesRecursive()

◆ replaceAndAppendScalarValuesRecursive()

static TYPO3\CMS\Core\Utility\ArrayUtility::replaceAndAppendScalarValuesRecursive ( array  $array1,
array  $array2 
)
static

Same as array_replace_recursive except that when in simple arrays (= YAML lists), the entries are appended (array_merge). The second array takes precedence in case of equal sub arrays.

Definition at line 971 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\processImports(), and TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\replaceAndAppendScalarValuesRecursiveCorrectlyMergesArrays().

◆ setValueByPath()

static array TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath ( array  $array,
string|array|\ArrayAccess  $path,
mixed  $value,
string  $delimiter = '/' 
)
static

Modifies or sets a new value in an array by given path

Example:

  • ‪array: array( 'foo' => array( 'bar' => 42, ), );
  • ‪path: foo/bar
  • ‪value: 23
  • ‪return: array( 'foo' => array( 'bar' => 23, ), );
Parameters
array$array‪Input array to manipulate
string | array | \ArrayAccess$path‪Path in array to search for
mixed$value‪Value to set at path location in array
string$delimiter‪Path delimiter
Returns
‪array Modified array
Exceptions

Definition at line 261 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\AddHmacDataToFormElementPropertyConverter\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\AddHmacDataToPropertyCollectionElementConverter\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FinisherTranslationLanguageConverter\__invoke(), TYPO3\CMS\Form\Domain\Configuration\FormDefinition\Converters\FinisherOptionsFlexFormOverridesConverter\__invoke(), TYPO3\CMS\Form\Domain\Model\FormDefinition\addElementDefaultValue(), TYPO3\CMS\Form\Domain\Finishers\FinisherVariableProvider\addOrUpdate(), TYPO3\CMS\Core\Site\SiteSettingsFactory\createSettings(), TYPO3\CMS\Install\Controller\SettingsController\extensionConfigurationWriteAction(), TYPO3\CMS\Form\Tests\Functional\Framework\FormHandling\FormData\getPostStructure(), TYPO3\CMS\Extbase\Tests\Functional\Mvc\Controller\ActionControllerArgumentTest\inflateFormValues(), TYPO3\CMS\Backend\Controller\Wizard\AddController\mainAction(), TYPO3\CMS\Dashboard\WidgetRegistry\registerWidget(), TYPO3\CMS\Form\Controller\FormFrontendController\renderAction(), TYPO3\CMS\Frontend\Tests\Functional\SiteHandling\EnhancerSiteRequest\RouteTest\routeIdentifiersAreResolved(), TYPO3\CMS\Form\Domain\Runtime\FormState\setFormValue(), TYPO3\CMS\Lowlevel\Command\MissingRelationsCommand\setReferenceValue_dbRels(), TYPO3\CMS\Lowlevel\Command\MissingRelationsCommand\setReferenceValue_softreferences(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\setValueByPathCanUseZeroAsPath(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\setValueByPathCanUseZeroAsPathSegment(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\setValueByPathSetsCorrectValue(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\setValueByPathThrowsExceptionIfPathIsEmpty(), TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\setValueByPathThrowsExceptionIfPathSegmentIsEmpty(), TYPO3\CMS\Form\Controller\FormEditorController\transformMultiValuePropertiesForFormEditor(), TYPO3\CMS\Core\DataHandling\DataHandler\updateFlexFormData(), and TYPO3\CMS\Core\Site\SiteSettingsFactory\validateSettings().

◆ sortArraysByKey()

static array TYPO3\CMS\Core\Utility\ArrayUtility::sortArraysByKey ( array  $arrays,
string  $key,
bool  $ascending = true 
)
static

Sort an array of arrays by a given key using uasort

Parameters
array$arrays‪Array of arrays to sort
string$key‪Key to sort after
bool$ascending‪Set to TRUE for ascending order, FALSE for descending order
Returns
‪array Array of sorted arrays
Exceptions

Definition at line 358 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\sortArraysByKeyCheckIfSortingIsCorrect(), and TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\sortArraysByKeyThrowsExceptionForNonExistingKey().

◆ sortByKeyRecursive()

static array TYPO3\CMS\Core\Utility\ArrayUtility::sortByKeyRecursive ( array  $array)
static

Sorts an array recursively by key

Parameters
array$array‪Array to sort recursively by key
Returns
‪array Sorted array

Definition at line 338 of file ArrayUtility.php.

Referenced by TYPO3\CMS\Lowlevel\Command\MissingRelationsCommand\findRelationsToNonExistingRecords(), and TYPO3\CMS\Core\Tests\Unit\Utility\ArrayUtilityTest\sortByKeyRecursiveCheckIfSortingIsCorrect().