PHP Reference

Quick Reference

The PHP array_column() function returns the values from a single column in the input array.

<?php
$a = array(
    array(
        'id' => 44,
        'first_name' => 'Sam',
        'last_name' => 'Martin',
    ),
    array(
        'id' => 57,
        'first_name' => 'Mike',
        'last_name' => 'Martin',
    ),
    array(
        'id' => 90,
        'first_name' => 'Gabe',
        'last_name' => 'Martin',
    )
);

$first_names = array_column($a, 'first_name');
print_r($first_names);
?>

Note

Arrays count starting from zero NOT one. So item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on.

Output

Array ( [0] => Sam [1] => Mike [2] => Gabe )

Syntax

array_column(array, column_key, index_key)

Parameters

ParameterDescription
arraySpecifies the array to use (required)
column_keyAn integer key or a string key name of the column of values to return; can also be NULL to return complete arrays (required)
index_keyThe column to use as the index/keys for the returned array

PHP Notes:

  • When using PHP, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.