[wp-trac] [WordPress Trac] #19888: We need a recursive version of wp_parse_args(), namely wp_parse_args_r()

WordPress Trac wp-trac at lists.automattic.com
Tue Jan 24 17:04:18 UTC 2012


#19888: We need a recursive version of wp_parse_args(), namely wp_parse_args_r()
--------------------------+-----------------------------
 Reporter:  Master Jake   |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 Currently, wp_parse_args() will merge a collection of possible arguments
 into a collection of defaults, overriding the defaults when necessary. The
 issue arises when one of the supplied arguments is an array itself (e.g.
 an array within an array). Intuitively, one might expect wp_parse_args to
 treat the inner array as its own instance and merge semi-separately, but
 it doesn't. We should add this functionality so that users (and possibly
 the core as well) can start taking advantage of it. It could follow PHP's
 standard naming convention of suffixing the function name with _r since
 it's recursive.

 An example using wp_parse_args():
 {{{
 #!php
 $args = array(
         'inner' => array(
                 'key1' => 'value1'
         )
 );

 $defaults = array(
         'rootkey1' => 'rootvalue1',
         'inner' => array(
                 'key2' => 'value2',
                 'key3' => 'value3'
         )
 );

 $merged = wp_parse_args( $args, $defaults );

 /*
 Contents of $merged:

 Array
 (
         [rootkey1] => rootvalue1
         [inner] => Array
                 (
                         [key1] => value1
                 )
 )

 */
 }}}

 The same example using the new wp_parse_args_r():
 {{{
 #!php
 $args = array(
         'inner' => array(
                 'key1' => 'value1'
         )
 );

 $defaults = array(
         'rootkey1' => 'rootvalue1',
         'inner' => array(
                 'key2' => 'value2',
                 'key3' => 'value3'
         )
 );

 $merged = wp_parse_args( $args, $defaults );

 /*
 Contents of $merged:

 Array
 (
         [rootkey1] => rootvalue1
         [inner] => Array
                 (
                         [key1] => value1
                         [key2] => value2
                         [key3] => value3
                 )
 )

 */
 }}}

 Notice the preserved content of the inner array. This new recursive
 function should perform this way for as many levels down as necessary
 (hence being a recursive function).

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/19888>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list