首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 5.5 新函數 array_column() 詳解與低版本 Polyfill

     編輯:PHP 相關知識

PHP 5.5 引入 array_column(),極大簡化二維數組列提取。本文詳細說明語法、參數、多種用法,並提供低版本 PHP 兼容實現,助你無縫升級。

函數簽名

array_column(array $array, int|string $column_key, int|string|null $index_key = null): array
  • $array:輸入二維數組(數組的數組)
  • $column_key:要提取的列鍵名(字符串)或索引(整數)
  • $index_key:可選,作為返回數組鍵的列;若為 null 保留原數字索引

三大典型用法

1. 提取關聯數組某列,保留數字索引

$records = [
    ['id' => 2135, 'first_name' => 'John', 'last_name' => 'Doe'],
    ['id' => 3245, 'first_name' => 'Sally', 'last_name' => 'Smith'],
    ['id' => 5342, 'first_name' => 'Jane', 'last_name' => 'Jones'],
    ['id' => 5623, 'first_name' => 'Peter', 'last_name' => 'Doe'],
];
$firstNames = array_column($records, 'first_name');
/*
Array (
    [0] => John
    [1] => Sally
    [2] => Jane
    [3] => Peter
)
*/

2. 數值索引數組提取指定列

$records = [
    [1, 'John', 'Doe'],
    [2, 'Sally', 'Smith'],
    [3, 'Jane', 'Jones'],
];
$lastNames = array_column($records, 2); // 索引 2
/*
Array (
    [0] => Doe
    [1] => Smith
    [2] => Jones
)
*/

3. 指定 index_key 重建鍵值(去重以最後出現為準)

$mismatched = [
    ['a' => 'foo', 'b' => 'bar', 'e' => 'baz'],
    ['a' => 'qux', 'c' => 'quux', 'd' => 'corge'],
    ['a' => 'grault', 'b' => 'garply', 'e' => 'waldo'],
];
$foo = array_column($mismatched, 'a', 'b');
/*
Array (
    [bar] => foo
    [0] => qux          // 第二行無 b 鍵,落回數字索引
    [garply] => grault
)
*/

低版本 Polyfill(PHP < 5.5)

<?php
if (!function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null) {
        $result = [];
        $colIsNum = is_numeric($columnKey);
        $idxIsNull = is_null($indexKey);
        $idxIsNum = is_numeric($indexKey);

        foreach ($input as $key => $row) {
            // 獲取列值
            if ($colIsNum) {
                $tmp = array_slice($row, $columnKey, 1);
                $val = (is_array($tmp) && $tmp) ? current($tmp) : null;
            } else {
                $val = isset($row[$columnKey]) ? $row[$columnKey] : null;
            }

            // 確定新鍵
            if (!$idxIsNull) {
                if ($idxIsNum) {
                    $tmp = array_slice($row, $indexKey, 1);
                    $newKey = (is_array($tmp) && $tmp) ? current($tmp) : 0;
                } else {
                    $newKey = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            } else {
                $newKey = $key;
            }

            $result[$newKey] = $val;
        }
        return $result;
    }
}

進階技巧

  • 配合 array_map:array_column($users, 'name', 'id') 直接得到 [id => name] 字典,省去循環
  • 提取嵌套屬性:需先 array_map(fn($u) => $u['profile']['avatar'], $users) 再 array_column
  • 內存效率:大數組時返回新數組會佔用雙倍內存,考慮生成器逐行處理

版本要求與替代方案

PHP 版本支援備選
>= 5.5.0內建—
5.3 - 5.4Polyfill上文自定義函數
< 5.3不支援foreach 手工構建

項目若已升級 PHP 5.5+,請直接使用內建函數,代碼更簡潔、性能更優(C 層實現)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved