欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解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)綁定默認值。

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

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

用法

wp_parse_args( $args, $defaults );

參數(shù)

$args

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

默認值:None

查詢字符串:

type=post&posts_per_page=5&cat=1

數(shù)組:

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

$defaults

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

默認值:空字符串

例子

function explain_parse_args( $args = array() ){
 
  //$args 的默認值
  $defaults = array(
    'before' => '<div class="box">',
    'after' => '</div>',
    'echo' => true,
    'text' => 'wp_parse_args() 函數(shù)演示'
  );
 
  //綁定默認值
  $r = wp_parse_args( $args, $defaults );
 
  $output = $r['before'] . $r['text'] . $r['after'];
  if( !$r['echo'] ) return $output;
  echo $output;
}
 
//沒有參數(shù)
explain_parse_args();//打?。?lt;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ù)來實現(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ù)為默認的預(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)文章

  • php 深入理解strtotime函數(shù)的使用詳解

    php 深入理解strtotime函數(shù)的使用詳解

    本篇文章是對php strtotime函數(shù)的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • php 代碼優(yōu)化的42條建議 推薦

    php 代碼優(yōu)化的42條建議 推薦

    php開發(fā)過程中,經(jīng)常需要考慮一些性能問題,下面是一些總結(jié)。
    2009-09-09
  • php項目打包方法

    php項目打包方法

    大家經(jīng)常會接到一些編程的活,例如設(shè)計企業(yè)網(wǎng)站,做做財務(wù),統(tǒng)計系統(tǒng)什么的。或許是因為朋友的需求,或許圖個零花。不管什么原因吧。等程序做好了,給對方展示、安裝,就成了問題。
    2008-09-09
  • php cli 小技巧

    php cli 小技巧

    很簡單,特別方便php做一些cli應(yīng)用的調(diào)試,需要的朋友可以參考下
    2013-06-06
  • PHP封裝的MSSql操作類完整實例

    PHP封裝的MSSql操作類完整實例

    這篇文章主要介紹了PHP封裝的MSSql操作類,以完整實例形式分析了php封裝的各種常用的mssql數(shù)據(jù)庫的操作,包括針對mssql數(shù)據(jù)庫的連接與增刪改查等,需要的朋友可以參考下
    2016-05-05
  • 將酷狗krc歌詞解析并轉(zhuǎn)換為lrc歌詞php源碼

    將酷狗krc歌詞解析并轉(zhuǎn)換為lrc歌詞php源碼

    這篇文章主要介紹了krc歌詞解析并轉(zhuǎn)換為lrc歌詞的php實現(xiàn)方法,需要的朋友可以參考下
    2014-06-06
  • PHP設(shè)計模式 注冊表模式

    PHP設(shè)計模式 注冊表模式

    注冊表模式其實是一個單例模式,注冊表類提供靜態(tài)方法(或單例對象的實例化方法)來讓其它對象訪問其中的數(shù)據(jù)(通常是對象)。整個系統(tǒng)中的每個對象都可以訪問這些數(shù)據(jù)對象
    2012-02-02
  • php面向?qū)ο缶幊蘳elf和static的區(qū)別

    php面向?qū)ο缶幊蘳elf和static的區(qū)別

    這篇文章主要介紹了PHP中static關(guān)鍵字以及與self關(guān)鍵字的區(qū)別,本文講解了static關(guān)鍵字的定義、遲綁定(Late Static Bindings)、以及與self關(guān)鍵字的區(qū)別等內(nèi)容,需要的朋友可以參考下
    2016-05-05
  • PHP中file_exists與is_file,is_dir的區(qū)別介紹

    PHP中file_exists與is_file,is_dir的區(qū)別介紹

    很顯然file_exists是受了asp的影響,因為asp不但有fileExists還有folderExists,driverExists,那么PHP中file_exists是什么意思呢
    2012-09-09
  • php利用ZipArchive類操作文件的實例

    php利用ZipArchive類操作文件的實例

    在本篇文章里小編給大家整理了關(guān)于php利用ZipArchive類實現(xiàn)文件壓縮與解壓實例代碼內(nèi)容,需要的朋友們學(xué)習下。
    2020-01-01

最新評論