當前位置: 首頁> 函數類別大全> array_column

array_column

返回輸入數組中單個列的值
名稱:array_column
分類:數組
所屬語言:php
一句話介紹:返回輸入數組中某個單一列的值。

PHP 函數: array_column

函數名

array_column

適用PHP 版本

PHP 5.5.0 及以上版本

函數說明

array_column函數從多維數組中返回指定列的值,常用於從記錄數組中提取特定字段。

函數語法

<span class="fun">array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array</span>

參數

  • $array :必需。輸入的多維數組(通常是記錄數組)。
  • $column_key :必需。你想要返回值的列名或索引。如果設為null ,將返回整個子數組。
  • $index_key :可選。用於作為返回數組的鍵的列名或索引。

返回值

返回包含指定列值的數組。如果提供了$index_key ,則使用其值作為返回數組的鍵。

示例

$records = [ [ &#39;id&#39; => 1, &#39;name&#39; => &#39;Alice&#39;, &#39;email&#39; => &#39;[email protected]&#39; ], [ &#39;id&#39; => 2, &#39;name&#39; => &#39;Bob&#39;, &#39;email&#39; => &#39;[email protected]&#39; ], [ &#39;id&#39; => 3, &#39;name&#39; => &#39;Charlie&#39;, &#39;email&#39; => &#39;[email protected]&#39; ], ]; $result = array_column($records, &#39;email&#39;);<br>
print_r($result);<br>

示例代碼的說明

此示例中, array_column$records中提取每個子數組的'email'字段,返回一個由email 地址組成的索引數組:

 Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] )
同類函數
  • 彈出數組最後一個單元(出棧) array_pop

    array_pop

    彈出數組最後一個單元(出棧)
  • 返回數組中的當前元素 current

    current

    返回數組中的當前元素
  • 通過使用一個數組作為鍵而另一個數組作為其值來創建數組 array_combine

    array_combine

    通過使用一個數組作為鍵而另一個數組作為其
  • 檢查給定的數組是否是一個列表 array_is_list

    array_is_list

    檢查給定的數組是否是一個列表
  • 從數組中將變量導入到當前的符號表 extract

    extract

    從數組中將變量導入到當前的符號表
  • 使用用戶自定義函數對數組中的每個元素做回調處理 array_walk

    array_walk

    使用用戶自定義函數對數組中的每個元素做回
  • current的別名 pos

    pos

    current的別名
  • 在數組開頭插入一個或多個單元 array_unshift

    array_unshift

    在數組開頭插入一個或多個單元
熱門文章