Home > Backend Development > PHP Tutorial > Detailed explanation of the wp_parse_args function used to synthesize arrays in WordPress

Detailed explanation of the wp_parse_args function used to synthesize arrays in WordPress

WBOY
Release: 2016-07-29 09:09:57
Original
1153 people have browsed it

The

wp_parse_args() function is a frequently used function in the core of WordPress. It has many uses, but it is mainly used to bind a default value to an array parameter (args).

Because the wp_parse_args() function must return an array, it will automatically convert the incoming query string and object into an array, giving users more convenient conditions and increasing compatibility.

The common query_posts(), wp_list_comments() and get_terms() functions all use the wp_parse_args() function to help them add default values ​​to array parameters.

Usage

wp_parse_args( $args, $defaults );
Copy after login

Parameters

$args

(array | string) (required) Query string, object or array parameters, used to bind default values.

Default: None

Query string:

type=post&posts_per_page=5&cat=1

Copy after login

Array:

array( 'type' => 'post', 'posts_per_page' => 5, 'cat' => '1' )

Copy after login

$defaults

(Array) (Optional) Default parameters for the array parameter.

Default value: empty string

Example

function explain_parse_args( $args = array() ){
 
  //$args 的默认值
  $defaults = array(
    'before' => '<div>',
    'after' => '</div>',
    'echo' => true,
    'text' => 'wp_parse_args() 函数演示'
  );
 
  //绑定默认值
  $r = wp_parse_args( $args, $defaults );
 
  $output = $r['before'] . $r['text'] . $r['after'];
  if( !$r['echo'] ) return $output;
  echo $output;
}
 
//没有参数
explain_parse_args();//打印:<div>wp_parse_args() 函数演示</div>
 
//字符串参数
$output = explain_parse_args( 'text=字符串参数&before=<div>&echo=0' );
echo $output;//打印:<div>字符串参数</div>
 
//数组参数
explain_parse_args( array( 'text' => '数组参数', 'before' => '<div>' ) );//打印:<div>数组参数</div>
还有另一种不使用第二个 $defaults 参数的用法,就是帮你把一个查询字符串、对象或者数组的变量直接转换成通用的数组,避免判断类型。

//字符串
$array = wp_parse_args( 'text=测试另一种用法&type=字符串' );
var_dump( $array );
/*
  array(2) {
    ["text"]=>
      string(21) "测试另一种用法"
    ["type"]=>
      string(9) "字符串"
  }
*/
 
//对象(object)
class args_obj{
 
  public $text = '测试另一种用法';
 
  public $type = '对象(object)';
 
  function func(){
    //转换成数组的时候对象里边的函数会被忽略
  }
 
}
$obj = new args_obj;
var_dump( $obj );
/*
object(args_obj)#2175 (2) {
  ["text"]=>
    string(21) "测试另一种用法"
  ["type"]=>
    string(18) "对象(object)"
}
*/

Copy after login

wp_parse_args function source code details The source code of this function:

/**
 * Merge user defined arguments into defaults array.
 *
 * This function is used throughout WordPress to allow for both string or array
 * to be merged into another array.
 *
 * @since 2.2.0
 *
 *第一个参数可以是 字符串、数组或对象(obj)
 * @param string|array $args Value to merge with $defaults
 *第二个参数为默认的预设值数组,必须是数组
 * @param array $defaults Array that serves as the defaults.
 *返回值将是一个数组
 * @return array Merged user defined values with defaults.
 */
function wp_parse_args( $args, $defaults = '' ) {
 if ( is_object( $args ) )
 //将接收的对象(obj)转换为数组
 $r = get_object_vars( $args );
 elseif ( is_array( $args ) )
 //如果是数组则不转换
 $r =& $args;
 else
 //将接收的字符串转换为数组
 wp_parse_str( $args, $r );
 if ( is_array( $defaults ) )
 return array_merge( $defaults, $r );
 return $r;
}
Copy after login

The get_object_vars function is used to return an associative array composed of object attributes. The

array_merge function is used to merge the cells of two or more arrays, and the values ​​in one array are appended to the previous array. Returns the resulting array.

The above has introduced a detailed explanation of the wp_parse_args function used to synthesize arrays in WordPress, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template