Make WordPress Core

Opened 7 months ago

Last modified 7 months ago

#59330 new enhancement

Improvement in wp_parse_args for mutli-dimensional arrays

Reported by: stachethemes's profile Stachethemes Owned by:
Milestone: Awaiting Review Priority: normal
Severity: trivial Version: 6.3.1
Component: General Keywords: has-patch
Focuses: Cc:

Description

I noticed the function doesn't work well when multi-dimensional arrays are present:

Example:

Consider having following default params and settings.

$defaults = array(
    'settings' => array(
        'option1' => 'default-value-1',
        'option2' => 'default-value-2'
    )
);

$settings = array(
    'settings' => array(
        'option1' => 'new-value-1',
    )
);

$return = wp_parse_args($settings, $defaults);

The above example will return:

Array
(
    [settings] => Array
        (
            [option1] => new-value-1
        )

)

It will completely ignore the option2 key.

My suggestion is to update wp_parse_args from:

if (is_array($defaults) && $defaults) {
    return array_merge($defaults, $parsed_args);
}

with:

if ( is_array( $defaults ) && $defaults ) {
    return array_replace_recursive($defaults, $parsed_args);
}

Change History (1)

This ticket was mentioned in PR #5229 on WordPress/wordpress-develop by zbozhilov.


7 months ago
#1

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.