詳解WordPress中用于合成數(shù)組的wp_parse_args()函數(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">' ) );//打印:<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)文章
學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)抽象工廠模式
這篇文章主要介紹了php設(shè)計(jì)模式中的抽象工廠模式,使用php實(shí)現(xiàn)抽象工廠模式,感興趣的小伙伴們可以參考一下2015-12-12
php中通過eval實(shí)現(xiàn)字符串格式的計(jì)算公式
有時候我們對每一種產(chǎn)品都有一個提成公式,而這個計(jì)算提成的公式是以字符串格式存在表中的,當(dāng)我們用這個計(jì)算公式時,他并不像我們寫的:$a=2+3*5;這樣簡單的能計(jì)算出結(jié)果,而它是個字符串,所以,我們就必須把字符串轉(zhuǎn)化為我們能夠處理的結(jié)果2017-03-03
Ajax PHP 邊學(xué)邊練 之三 數(shù)據(jù)庫
在上一篇備忘日歷實(shí)例中,實(shí)現(xiàn)了當(dāng)鼠標(biāo)放在某個日期上時,如果當(dāng)天有備忘信息則會顯示出來,但是這些信息是為了測試方便事先寫在數(shù)組中的數(shù)據(jù),不能體現(xiàn)其實(shí)時性。2009-11-11
PHP 在5.1.* 和5.2.*之間 PDO數(shù)據(jù)庫操作中的不同之處小結(jié)
今天發(fā)現(xiàn)php5.1.*和php5.2.*在數(shù)據(jù)庫預(yù)編譯代碼執(zhí)行的時候出現(xiàn)差異2012-03-03
php實(shí)現(xiàn)在服務(wù)器上創(chuàng)建目錄的方法
這篇文章主要介紹了php實(shí)現(xiàn)在服務(wù)器上創(chuàng)建目錄的方法,實(shí)例分析了php中使用mkdir函數(shù)的使用技巧,需要的朋友可以參考下2015-03-03
php中使用PHPExcel讀寫excel(xls)文件的方法
這篇文章主要介紹了php中使用PHPExcel讀寫excel(xls)文件的方法,phpExcel是常用的用于操作Excel的PHP類庫,應(yīng)用非常廣泛。需要的朋友可以參考下2014-09-09
PHP數(shù)組 為文章加關(guān)鍵字連接 文章內(nèi)容自動加鏈接
PHP給文章加關(guān)鍵字連接,像163文章內(nèi)容自動加鏈接效果,其實(shí)很多php網(wǎng)站管理系統(tǒng)里面都有,可以參考里面的代碼。2011-12-12

