PHP實(shí)現(xiàn)提取多維數(shù)組指定一列的方法總結(jié)
本文實(shí)例講述了PHP實(shí)現(xiàn)提取多維數(shù)組指定一列的方法。分享給大家供大家參考,具體如下:
PHP中對多維數(shù)組特定列的提取,是個很常用的功能,正因?yàn)槿绱?,PHP在5.5.0版本之后,添加了一個專用的函數(shù)array_column()。當(dāng)然,如果你的PHP版本低于5.5.0,就得用別的方法處理了。
例如,對于以下這個數(shù)組:
$user = array( '0' => array('id' => 100, 'username' => 'a1'), '1' => array('id' => 101, 'username' => 'a2'), '2' => array('id' => 102, 'username' => 'a3'), '3' => array('id' => 103, 'username' => 'a4'), '4' => array('id' => 104, 'username' => 'a5'), );
我們要提取其中的 usename 列,變成:
$username = array('a1', 'a2', 'a3', 'a4', 'a5');
方法有以下幾種。
1 array_column函數(shù)法
用PHP內(nèi)置的 array_column() 函數(shù)是最簡單的方法,限制是PHP版本必須是5.5.0及以上版本,方法:
$username = array_column($user, 'username');
2 array_walk函數(shù)法
array_walk()函數(shù)使用用戶自定義函數(shù)對數(shù)組中的每個元素做回調(diào)處理,實(shí)現(xiàn)當(dāng)前功能的方法:
$username = array(); array_walk($user, function($value, $key) use (&$username){ $username[] = $value['username']; });
3 array_map函數(shù)法
array_map()函數(shù)和array_walk() 作用類似,將回調(diào)函數(shù)作用到給定數(shù)組的單元上。
$username = array(); array_map(function($value) use (&$username){ $username[] = $value['username']; }, $user);
實(shí)際使用時,我們可以用array_map()寫出和PHP內(nèi)置array_column()一樣功能的函數(shù):
/** * 獲取二維數(shù)組指定的一列,并以一維數(shù)組格式返回 * 作用和PHP5.5.0中的array_column()函數(shù)一樣 * @param $input array 需要取出數(shù)組列的多維數(shù)組(或結(jié)果集) * @param $column_key string 需要返回值的列,它可以是索引數(shù)組的列索引,或者是關(guān)聯(lián)數(shù)組的列的鍵。 也可以是NULL,此時將返回整個數(shù)組 * @param $index_key string 作為返回數(shù)組的索引/鍵的列,它可以是該列的整數(shù)索引,或者字符串鍵值。 * @return array|null */ function array_column($input, $column_key, $index_key = null) { $arr = array_map(function($d) use ($column_key, $index_key) { if (!isset($d[$column_key])) { return null; } if ($index_key !== null) { return array($d[$index_key] => $d[$column_key]); } return $d[$column_key]; }, $input); if ($index_key !== null) { $tmp = array(); foreach ($arr as $ar) { $tmp[key($ar)] = current($ar); } $arr = $tmp; } return $arr; }
4 foreach循環(huán)法
foreach循環(huán)相對上面的方法效率稍微低一些,但簡單容易理解。
$username = array(); foreach ($user as $value) { $username[] = $value['username']; }
5 array_map變種
方法如下,意為把$user數(shù)組的每一項(xiàng)值的開頭值移出,并獲取移除的值作為新數(shù)組。注意此時新數(shù)組$username的鍵仍是原數(shù)組$user的鍵,如下。
$username = array_map('array_shift', $user);
注意:該功能會獲取$user中的 id 列,而不是 username 列。
另外,如果需要獲取二維數(shù)組每一項(xiàng)的開頭列或結(jié)尾列,也可以這樣做:
$username = array_map('reset', $user); $username = array_map('end', $user);
這三個變種方法作用比較局限,僅在獲取第一列或最后一列的時候有用,在復(fù)雜的數(shù)組中就難以發(fā)揮作用了。
參考資料:
- php獲取二維數(shù)組中某一列的值集合
- php 快速的對二維數(shù)組某一列進(jìn)行組裝的方法
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《php字符串(string)用法總結(jié)》及《PHP常用遍歷算法與技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計有所幫助。
上一篇:PHP 圖像處理與SESSION制作超簡單驗(yàn)證碼的方法示例
欄 目:PHP編程
下一篇:PHP實(shí)現(xiàn)發(fā)送微博消息功能完整示例
本文標(biāo)題:PHP實(shí)現(xiàn)提取多維數(shù)組指定一列的方法總結(jié)
本文地址:http://mengdiqiu.com.cn/a1/PHPbiancheng/11066.html
您可能感興趣的文章
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂碼
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)語句
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)據(jù)函數(shù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方案
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文件的語句


閱讀排行
本欄相關(guān)
- 04-02php本站才可以請求數(shù)據(jù) php本地數(shù)據(jù)庫
- 04-02關(guān)于txt數(shù)據(jù)庫php的信息
- 04-02php打印請求數(shù)據(jù) php打印輸出結(jié)果
- 04-02網(wǎng)頁里php操作數(shù)據(jù)庫 php網(wǎng)頁例子
- 04-02php插入數(shù)據(jù)庫為亂碼 php連接數(shù)據(jù)庫亂
- 04-02php數(shù)據(jù)庫地址 phpstudy 數(shù)據(jù)庫
- 04-02php數(shù)據(jù)庫數(shù)據(jù)相加 php數(shù)據(jù)庫添加數(shù)據(jù)
- 04-02數(shù)據(jù)權(quán)限架構(gòu)思路php 數(shù)據(jù)權(quán)限設(shè)計方
- 04-02php數(shù)據(jù)庫輸入變量 php里輸出數(shù)據(jù)庫數(shù)
- 04-02php如何用導(dǎo)入數(shù)據(jù) php用來導(dǎo)入其他文
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery