YouTip LogoYouTip

Func Array Column

PHP array_column() Function

PHP array_column() Function

-- Learning is not just about technology, but also about dreams!

PHP Tutorial

PHP Tutorial PHP Introduction PHP Installation PHP Syntax PHP Variables PHP echo/print PHP EOF(heredoc) PHP Data Types PHP Type Comparisons PHP Constants PHP String PHP Operators PHP If...Else PHP Switch PHP Arrays PHP Array Sorting PHP Superglobals PHP While Loop PHP For Loop PHP Functions PHP Magic Constants PHP Namespaces PHP OOP PHP Quiz

PHP Forms

PHP Forms PHP Form Validation PHP Form - Required Fields PHP Form - Validate Email and URL PHP Complete Form Example PHP $_GET Variable PHP $_POST Variable

PHP Advanced Tutorial

PHP Multidimensional Arrays PHP Date PHP Include PHP File PHP File Upload PHP Cookie PHP Session PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter PHP Advanced Filters PHP JSON

PHP 7 New Features

PHP 7 New Features

PHP Database

PHP MySQL Introduction PHP MySQL Connect PHP MySQL Create Database PHP MySQL Create Table PHP MySQL Insert Data PHP MySQL Insert Multiple Data PHP MySQL Prepared Statements PHP MySQL Read Data PHP MySQL Where PHP MySQL Order By PHP MySQL Update PHP MySQL Delete PHP ODBC

PHP XML

XML Expat Parser XML DOM XML SimpleXML

PHP and AJAX

AJAX Introduction AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

PHP Reference Manual

PHP Array PHP Calendar PHP cURL PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP PDO PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones PHP Image Processing PHP RESTful PHP PCRE PHP Available Functions PHP Composer

PHP Example AJAX Poll

PHP 5 Calendar Functions

Explore Further

  • Programming
  • Web Services
  • Web Service
  • Computer Science
  • Web Design & Development
  • Programming Languages
  • Development Tools
  • Scripting
  • Scripting Languages
  • Software

PHP array_column() Function

PHP Array Reference Complete PHP Array Reference

Example

Extract the last_name column from the recordset:

<?php

// Possibly returned from a 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
(
     => Griffin
     => Smith
     => Doe
)

Definition and Usage

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


Syntax

array_column(array, column_key, index_key);
Parameter Description
array Required. Specifies the multidimensional array (recordset) to use.
column_key Required. The column of values to return. This value may be the integer key of the column you wish to retrieve, or the string key name for an associative array. It may also be NULL, in which case the entire array will be returned (useful in combination with index_key to reindex the array).
index_key Optional. The column to use as the keys/indexes for the returned array.

Technical Details

Return Value: Returns an array of values representing a single column from the input array.
PHP Version: 5.5+

More Examples

Example 1

Extract the last_name column from the recordset, using the corresponding "id" column as the keys:

<?php

// Possibly returned from a 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
(
     => Griffin
     => Smith
     => Doe
)

PHP Array Reference Complete PHP Array Reference

← Func Array ReplaceProp Keygen Type β†’