Removes the properties a spec names from the current value.
Description
The mirror of merge_properties(), applied at every nesting level: a list in $spec names entries to delete from $current — associative keys are unset, and list members are matched by identity (list_item_identity) and dropped — while an associative $spec recurses into each named entry to prune from within it. A name absent from $current is ignored, and a list is renumbered after members are removed so it keeps sequential keys.
Parameters
$currentmixedrequired- The current value.
$specmixedrequired- The names to remove from it.
Source
private function remove_properties( $current, $spec ) {
if ( ! is_array( $current ) || ! is_array( $spec ) ) {
return $current;
}
$current_is_list = array_is_list( $current );
if ( array_is_list( $spec ) ) {
// Each entry names something to delete from the current value.
foreach ( $spec as $name ) {
if ( $current_is_list ) {
$current = $this->remove_list_member( $current, $name );
} else {
unset( $current[ $name ] );
}
}
} else {
// Each key names an entry to recurse into and prune from within.
foreach ( $spec as $name => $subspec ) {
if ( $current_is_list ) {
foreach ( $current as $index => $member ) {
if ( $this->list_item_identity( $member ) === (string) $name ) {
$current[ $index ] = $this->remove_properties( $member, $subspec );
break;
}
}
} elseif ( array_key_exists( $name, $current ) ) {
$current[ $name ] = $this->remove_properties( $current[ $name ], $subspec );
}
}
}
// Renumber so a list from which a member was removed keeps sequential keys.
return $current_is_list ? array_values( $current ) : $current;
}
Changelog
| Version | Description |
|---|---|
| 7.1.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.