PHP array_column() function

Halimbawa

Makuha ang kolum na last_name mula sa record set:

<?php
// Indicates the array of possible records returned by the database
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Bill',
    'last_name' => 'Gates',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Steve',
    'last_name' => 'Jobs',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Mark',
    'last_name' => 'Zuckerberg',
  )
);
$last_names = array_column($a, 'last_name');
print_r($last_names);
?>

Output:

Array
(
  [0] => Gates
  [1] => Jobs
  [2] => Zuckerberg
)

paglilinaw at paggamit

Ang array_column() ay nagbibigay ng halaga ng isang solong kolum ng input array.

gramatika

array_column(array,column_key,index_key);
parameter paglalarawan
array Wastuhin. Itinuturing ang gamit na maraming-dimensiyonal na array (record set).
column_key

Required. The column that needs to return the value.

It can be an integer index of the index array or a string key value of the associated array.

This parameter can also be NULL, in which case the entire array will be returned (very useful when resetting the array keys with the index_key parameter).

index_key Optional. Used as the index/key of the returned array.

Technical Details

Return Value: Returns an array, the value of which is the value of a single column in the input array.
PHP Version: 5.5+

More Examples

Example 1

Extract the 'last_name' column from the record set, using the corresponding 'id' column as the key value:

<?php
// Indicates the array of possible records returned by the database
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Bill',
    'last_name' => 'Gates',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Steve',
    'last_name' => 'Jobs',
  )
  array(
    'id' => 3809,
    'first_name' => 'Mark',
    'last_name' => 'Zuckerberg',
  )
);
$last_names = array_column($a, 'last_name', 'id');
print_r($last_names);
?>

Output:

Array
(
  [5698] => Gates
  [4767] => Jobs
  [3809] => Zuckerberg
)