Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Component/Utility/NestedArray.php \Drupal\Component\Utility\NestedArray::filter()
  2. 9 core/lib/Drupal/Component/Utility/NestedArray.php \Drupal\Component\Utility\NestedArray::filter()

Filters a nested array recursively.

Parameters

array $array: The filtered nested array.

callable|null $callable: The callable to apply for filtering.

Return value

array The filtered array.

2 calls to NestedArray::filter()
NestedArrayTest::testFilter in core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php
@covers ::filter @dataProvider providerTestFilter
TestDiscovery::getTestClasses in core/lib/Drupal/Core/Test/TestDiscovery.php
Discovers all available tests in all extensions.

File

core/lib/Drupal/Component/Utility/NestedArray.php, line 358

Class

NestedArray
Provides helpers to perform operations on nested arrays and array keys of variable depth.

Namespace

Drupal\Component\Utility

Code

public static function filter(array $array, callable $callable = NULL) {
  $array = is_callable($callable) ? array_filter($array, $callable) : array_filter($array);
  foreach ($array as &$element) {
    if (is_array($element)) {
      $element = static::filter($element, $callable);
    }
  }
  return $array;
}