最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解WordPress中用于合成數(shù)組的wp_parse_args()函數(shù)

 更新時間:2015年12月18日 14:43:22   作者:斌果  
這篇文章主要介紹了WordPress中用于合成數(shù)組的wp_parse_args()函數(shù),轉(zhuǎn)換成數(shù)組通常是為了方便查詢,需要的朋友可以參考下

wp_parse_args() 函數(shù)是 WordPress 核心經(jīng)常用到的函數(shù),它的用途很多,但最主要用來給一個數(shù)組參數(shù)(args)綁定默認(rèn)值。

因?yàn)?wp_parse_args() 函數(shù)返回的一定是一個數(shù)組,所以他會把傳入查詢字符串和對象(object)自動轉(zhuǎn)換成數(shù)組,給了使用者更加方便的條件,也增加了兼容性。

常見的 query_posts()、wp_list_comments() 和 get_terms() 函數(shù)都使用了 wp_parse_args() 函數(shù)來幫它給數(shù)組參數(shù)添加默認(rèn)值。

用法

wp_parse_args( $args, $defaults );

參數(shù)

$args

(數(shù)組 | 字符串)(必須)查詢字符串、對象或者數(shù)組參數(shù),用來綁定默認(rèn)值。

默認(rèn)值:None

查詢字符串:

type=post&posts_per_page=5&cat=1

數(shù)組:

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

$defaults

(數(shù)組)(可選)數(shù)組參數(shù)的默認(rèn)參數(shù)。

默認(rèn)值:空字符串

例子

function explain_parse_args( $args = array() ){
 
  //$args 的默認(rèn)值
  $defaults = array(
    'before' => '<div class="box">',
    'after' => '</div>',
    'echo' => true,
    'text' => 'wp_parse_args() 函數(shù)演示'
  );
 
  //綁定默認(rèn)值
  $r = wp_parse_args( $args, $defaults );
 
  $output = $r['before'] . $r['text'] . $r['after'];
  if( !$r['echo'] ) return $output;
  echo $output;
}
 
//沒有參數(shù)
explain_parse_args();//打印:<div class="box">wp_parse_args() 函數(shù)演示</div>
 
//字符串參數(shù)
$output = explain_parse_args( 'text=字符串參數(shù)&before=<div class="box-2">&echo=0' );
echo $output;//打?。?lt;div class="box-2">字符串參數(shù)</div>
 
//數(shù)組參數(shù)
explain_parse_args( array( 'text' => '數(shù)組參數(shù)', 'before' => '<div class="box-3">' ) );//打?。?lt;div class="box-3">數(shù)組參數(shù)</div>
還有另一種不使用第二個 $defaults 參數(shù)的用法,就是幫你把一個查詢字符串、對象或者數(shù)組的變量直接轉(zhuǎn)換成通用的數(shù)組,避免判斷類型。

//字符串
$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(){
    //轉(zhuǎn)換成數(shù)組的時候?qū)ο罄镞叺暮瘮?shù)會被忽略
  }
 
}
$obj = new args_obj;
var_dump( $obj );
/*
object(args_obj)#2175 (2) {
  ["text"]=>
    string(21) "測試另一種用法"
  ["type"]=>
    string(18) "對象(object)"
}
*/

wp_parse_args函數(shù)源代碼詳解
wp_parse_args 函數(shù)的源代碼比較簡單,
依附于PHP 內(nèi)置函數(shù)get_object_vars、array_merge與WordPress的wp_parse_str函數(shù)來實(shí)現(xiàn),
以下是該函數(shù)的源代碼:

/**
 * 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
 *
 *第一個參數(shù)可以是 字符串、數(shù)組或?qū)ο螅╫bj)
 * @param string|array $args Value to merge with $defaults
 *第二個參數(shù)為默認(rèn)的預(yù)設(shè)值數(shù)組,必須是數(shù)組
 * @param array $defaults Array that serves as the defaults.
 *返回值將是一個數(shù)組
 * @return array Merged user defined values with defaults.
 */
function wp_parse_args( $args, $defaults = '' ) {
 if ( is_object( $args ) )
 //將接收的對象(obj)轉(zhuǎn)換為數(shù)組
 $r = get_object_vars( $args );
 elseif ( is_array( $args ) )
 //如果是數(shù)組則不轉(zhuǎn)換
 $r =& $args;
 else
 //將接收的字符串轉(zhuǎn)換為數(shù)組
 wp_parse_str( $args, $r );
 if ( is_array( $defaults ) )
 return array_merge( $defaults, $r );
 return $r;
}

其中g(shù)et_object_vars函數(shù)是用來返回由對象屬性組成的關(guān)聯(lián)數(shù)組。
array_merge函數(shù)用是將兩個或多個數(shù)組的單元合并起來,一個數(shù)組中的值附加在前一個數(shù)組的后面。返回作為結(jié)果的數(shù)組。

相關(guān)文章

最新評論

上蔡县| 铜鼓县| 宁陕县| 沁水县| 徐闻县| 长岭县| 淳安县| 常熟市| 安新县| 诸暨市| 津南区| 温泉县| 江达县| 达孜县| 麻江县| 罗山县| 丹阳市| 大理市| 郧西县| 论坛| 英德市| 田东县| 瑞丽市| 和田市| 舞阳县| 黄大仙区| 富源县| 柳林县| 定州市| 扶余县| 石门县| 阿城市| 饶平县| 阜宁县| 汕头市| 蒲江县| 洛宁县| 孟村| 云龙县| 洛隆县| 盐池县|