array_column — 返回數組中指定的一列 說明 array_column ( array $input , mixed $column_key [, mixed $index_key = null ] ) : array array_column() 返回input數組中鍵值為column_key的列, 如果指定了可選參數index_key,那么input數組中的這一列的值將作為返回數組中對應值的鍵。 參數 input 需要取出數組列的多維數組。 如果提供的是包含一組對象的數組,只有 public 屬性會被直接取出。 為了也能取出 private 和 protected 屬性,類必須實現 __get() 和 __isset() 魔術方法。 column_key 需要返回值的列,它可以是索引數組的列索引,或者是關聯數組的列的鍵,也可以是屬性名。 也可以是NULL,此時將返回整個數組(配合index_key參數來重置數組鍵的時候,非常管用) index_key 作為返回數組的索引/鍵的列,它可以是該列的整數索引,或者字符串鍵值。
// 測試二維數組 $arr = array ( 0 => array ( 'value' => 1, 'name' => 'test_0', ), 1 => array ( 'value' => 2, 'name' => 'test_1', ), 2 => array ( 'value' => 3, 'name' => 'test_2', ), 3 => array ( 'value' => 4, 'name' => 'test_3', ), 4 => array ( 'value' => 5, 'name' => 'test_4', ), ); // 以name字段為鍵名 $tempArr = array_column($arr, null, 'name'); array ( 'test_0' => array ( 'value' => 1, 'name' => 'test_0', ), 'test_1' => array ( 'value' => 2, 'name' => 'test_1', ), 'test_2' => array ( 'value' => 3, 'name' => 'test_2', ), 'test_3' => array ( 'value' => 4, 'name' => 'test_3', ), 'test_4' => array ( 'value' => 5, 'name' => 'test_4', ), )