Recursively drops every property whose value is null from a value.
Description
set() swaps a named key’s value in wholesale rather than merging it into the current one, so it has no existing leaf for a nested null to delete the way merge() and replace() do. Stripping nulls here gives a nested null the same “drop the property it names” meaning under set() that it carries everywhere else. The same applies to a list replace() swaps in wholesale. A list is renumbered after a member is removed so removed entries do not leave gaps.
Parameters
$valuemixedrequired- The value to strip nulls from.
Source
private function strip_nulls( $value ) {
if ( ! is_array( $value ) ) {
return $value;
}
$result = array();
foreach ( $value as $key => $item ) {
// A null value drops the property it names.
if ( null === $item ) {
continue;
}
$result[ $key ] = $this->strip_nulls( $item );
}
// Renumber a list so a removed member does not leave a gap.
return array_is_list( $value ) ? array_values( $result ) : $result;
}
Changelog
| Version | Description |
|---|---|
| 7.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.