wp_list_pluck

函式
wp_list_pluck ( $list, $field, $index_key = null )
引數
  • (array) $list List of objects or arrays.
    Required:
  • (int|string) $field Field from the object to place instead of the entire object.
    Required:
  • (int|string) $index_key Optional. Field from the object to use as keys for the new array. Default null.
    Required:
    Default: null
返回值
  • (array) Array of found values. If `$index_key` is set, an array of found values with keys corresponding to `$index_key`. If `$index_key` is null, array keys from the original `$list` will be preserved in the results.
定義位置
相關方法
wp_list_catswp_list_pageswp_set_post_lockwp_list_userswp_insert_link
引入
3.1.0
棄用
-

wp_list_pluck: 這個函式從一個物件陣列中提取一個特定的欄位並返回一個包含該欄位值的陣列。它對於從WordPress函式(如get_posts)返回的陣列中提取資料很有用。

從陣列中的每個物件或陣列中抽取某個欄位。

這與array_column()(PHP 5.5)的功能和原型相同,但也支援物件。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_list_pluck( $list, $field, $index_key = null ) {
if ( ! is_array( $list ) ) {
return array();
}
$util = new WP_List_Util( $list );
return $util->pluck( $field, $index_key );
}
function wp_list_pluck( $list, $field, $index_key = null ) { if ( ! is_array( $list ) ) { return array(); } $util = new WP_List_Util( $list ); return $util->pluck( $field, $index_key ); }
function wp_list_pluck( $list, $field, $index_key = null ) {
	if ( ! is_array( $list ) ) {
		return array();
	}

	$util = new WP_List_Util( $list );

	return $util->pluck( $field, $index_key );
}

常見問題

FAQs
檢視更多 >