《PHP實戰:PHP獲取數組中單列值的方法》要點:
本文介紹了PHP實戰:PHP獲取數組中單列值的方法,希望對您有用。如果有疑問,可以聯系我們。
本文實例講述了PHP獲取數組中單列值的方法.分享給大家供大家參考,具體如下:PHP實戰
PHP中獲取數組中單列的值如下:PHP實戰
利用PHP中的數組函數 array_column()
:返回數組中某個單列的值.(PHP 5.5+適用)PHP實戰
語法:PHP實戰
array_column(array,column_key,index_key);
PHP實戰
參數:PHP實戰
array : 必需,規定必須為多維數組;
column_key : 必需,需要返回的值的鍵名;可以是索引數組的列的整數索引,或者是關聯數組的列的字符串鍵值.該參數也可以是 NULL,此時將返回整個數組(配合 index_key 參數來重置數組鍵的時候,非常有用).
index_key : 可選.用作返回數組的索引/鍵的列.PHP實戰
實例:PHP實戰
從記錄集中取出 last_name 列,用相應的 "id" 列作為鍵值:PHP實戰
<?php // 表示由數據庫返回的可能記錄集的數組 $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); ?>
輸出:PHP實戰
Array ( [5698] => Gates [4767] => Jobs [3809] => Zuckerberg )
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結》、《PHP數據結構與算法教程》、《php程序設計算法總結》、《php字符串(string)用法總結》及《php常見數據庫操作技巧匯總》PHP實戰
希望本文所述對大家PHP程序設計有所幫助.PHP實戰
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/648.html