Get the last_name column from the recordset:
<?php// It is possible to return an array from the database $a = array( array( 'id' => 5698, 'first_name' => 'Peter', 'last_name' => 'Griffin', ), array( 'id' => 4767, 'first_name' => 'Ben', 'last_name' => 'Smith', ), array( 'id' => 3809, 'first_name' => 'Joe', 'last_name' => 'Doe', ));$last_names = array_column($a, 'last_name');print_r($last_names);?>Output:
Array( [0] => Griffin [1] => Smith [2] => Doe)array_column() returns the value of a single column in the input array.
array_column( array , column_key , index_key );
parameter | describe |
---|---|
array | Required. Specifies the multidimensional array (record set) to use. |
column_key | Required. The column whose value needs to be returned. Can be an integer index of a column of an index array, or a string key value of a column of an associative array. This parameter can also be NULL, in which case the entire array will be returned (very useful when used with the index_key parameter to reset the array key). |
index_key | Optional. The column that is the index/key of the returned array. |
Return value: | Returns an array containing the value of a single column in the input array. |
---|---|
PHP version: | 5.5+ |
Take the last_name column from the recordset and use the corresponding "id" column as the key value:
<?php// It is possible to return an array from the database $a = array( array( 'id' => 5698, 'first_name' => 'Peter', 'last_name' => 'Griffin', ), array( 'id' => 4767, 'first_name' => 'Ben', 'last_name' => 'Smith', ), array( 'id' => 3809, 'first_name' => 'Joe', 'last_name' => 'Doe', ));$last_names = array_column($a, 'last_name', 'id');print_r($last_names);?>Output:
Array( [5698] => Griffin [4767] => Smith [3809] => Doe)