Tribe__Utils__Array::array_visit_recursive( array $input, callable $visitor )
Recursively visits all elements of an array applying the specified callback to each element key and value.
Contents
Parameters
- $input
-
(array) (Required) The input array whose nodes should be visited.
- $visitor
-
(callable) (Required) A callback function that will be called on each array item; the callback will receive the item key and value as input and should return an array that contains the update key and value in the shape
[ <key>, <value> ]. Returning anullkey will cause the element to be removed from the array.
Source
File: src/Tribe/Utils/Array.php
public static function array_visit_recursive( $input, callable $visitor ) {
if ( ! is_array( $input ) ) {
return $input;
}
$return = [];
foreach ( $input as $key => &$value ) {
if ( is_array( $value ) ) {
$value = static::array_visit_recursive( $value, $visitor );
}
// Ensure visitors can quickly return `null` to remove an element.
list( $updated_key, $update_value ) = array_replace( [ $key, $value ], (array) $visitor( $key, $value ) );
if ( false === $updated_key ) {
// Visitor will be able to remove an element by returning a `false` key for it.
continue;
}
if ( null === $updated_key ) {
// Automatically assign the first available numeric index to the element.
$return[] = $update_value;
} else {
$return[ $updated_key ] = $update_value;
}
}
return $return;
}
Changelog
| Version | Description |
|---|---|
| 4.12.14 | Introduced. |