[wp-trac] [WordPress Trac] #59330: Improvement in wp_parse_args for mutli-dimensional arrays
WordPress Trac
noreply at wordpress.org
Tue Sep 12 13:36:22 UTC 2023
#59330: Improvement in wp_parse_args for mutli-dimensional arrays
--------------------------+-----------------------------
Reporter: Stachethemes | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: 6.3.1
Severity: trivial | Keywords:
Focuses: |
--------------------------+-----------------------------
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);
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/59330>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list