本篇文章為大家展示了利用PHP在對數(shù)組中的單列值進行獲取,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
PHP中獲取數(shù)組中單列的值如下:
利用PHP中的數(shù)組函數(shù)array_column()
:返回數(shù)組中某個單列的值。(PHP 5.5+適用)
語法:
array_column(array,column_key,index_key);
參數(shù):
array : 必需,規(guī)定必須為多維數(shù)組;
column_key : 必需,需要返回的值的鍵名;可以是索引數(shù)組的列的整數(shù)索引,或者是關(guān)聯(lián)數(shù)組的列的字符串鍵值。該參數(shù)也可以是 NULL,此時將返回整個數(shù)組(配合 index_key 參數(shù)來重置數(shù)組鍵的時候,非常有用)。
index_key : 可選。用作返回數(shù)組的索引/鍵的列。
實例:
從記錄集中取出 last_name 列,用相應的 "id" 列作為鍵值:
<?php // 表示由數(shù)據(jù)庫返回的可能記錄集的數(shù)組 $a = array( array( 'id' => 5698, 'first_name' => 'Bill', 'last_name' => 'Gates', ), array( 'id' => 4767, 'first_name' => 'Steve', 'last_name' => 'Jobs', ) array( 'id' => 3809, 'first_name' => 'Mark', 'last_name' => 'Zuckerberg', ) ); $last_names = array_column($a, 'last_name', 'id'); print_r($last_names); ?>