PHP array_column: How to Extract Values from Arrays

php array column

The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull out numbers, strings, or any column value you need.

What is the array_column Function in PHP?

The array_column function gets values from a single column in an array. It can also return those values with new keys if you set a key field.

Here is the syntax:

array_column($array, $column_key, $index_key)
  • array: input array
  • column_key: column index or key that you want to extract
  • index_key: optional key to use for the result array

The function always returns a new array.

You need to take values from a big array. Instead of loops you use array_column.

Here is a quick example:

$data = [
    ["id" => 1, "name" => "Ali"],
    ["id" => 2, "name" => "Sara"],
    ["id" => 3, "name" => "Omar"]
];

$result = array_column($data, "name");
print_r($result);

This returns only the names as one array. You get the final output here:

Array
(
[0] => Ali
[1] => Sara
[2] => Omar
)

The function looks at each inner array. It finds the field that matches your column key. It then creates a new array with only those values.

Here is another example:

$users = [
    ["id" => 11, "email" => "ali@mail.com"],
    ["id" => 22, "email" => "sara@mail.com"],
    ["id" => 33, "email" => "omar@mail.com"]
];

$emails = array_column($users, "email");
print_r($emails);

This gives you only the email column. The result is simple and direct. Here is the output:

Array
(
[0] => ali@mail.com
[1] => sara@mail.com
[2] => omar@mail.com
)

Use array_column with Associative Keys in PHP

You can also set a key field. That field becomes the index of the result array. For example:

$users = [
    ["id" => 101, "name" => "Mona"],
    ["id" => 102, "name" => "Hassan"]
];

$result = array_column($users, "name", "id");
print_r($result);

This returns names with ids as array keys. It gives you a direct map of id to name.

The output:

Array
(
[101] => Mona
[102] => Hassan
)

Here is another example:

$items = [
    ["code" => "A1", "price" => 50],
    ["code" => "B2", "price" => 80],
    ["code" => "C3", "price" => 100]
];

$result = array_column($items, "price", "code");
print_r($result);

This creates a new array. Each item code becomes the key, and each price becomes the value.

Here is the output:

Array
(
[A1] => 50
[B2] => 80
[C3] => 100
)

Handle Duplicate Values with array_column

The function does not check for duplicate values. If two rows have the same key, the last one replaces the first one.

$items = [
    ["code" => "A1", "price" => 50],
    ["code" => "A1", "price" => 70]
];

$result = array_column($items, "price", "code");
print_r($result);

This gives you only one key, A1, with a value of 70. The output:

Array
(
[A1] => 70
)

Examples

Extract product names:

$products = [
    ["id" => 1, "name" => "Phone"],
    ["id" => 2, "name" => "Tablet"],
    ["id" => 3, "name" => "Laptop"]
];

$result = array_column($products, "name");
print_r($result);

This example takes only the name field from each product array and returns one array with those names. It avoids loops and creates one simple output:

Array
(
[0] => Phone
[1] => Tablet
[2] => Laptop
)

Create a user map by email:

$users = [
    ["id" => 11, "email" => "ali@mail.com"],
    ["id" => 12, "email" => "sara@mail.com"]
];

$result = array_column($users, "id", "email");
print_r($result);

This example makes each email the key and the id the value. It gives you an easy way to look up ids by email directly.

Here is the output:

Array
(
[ali@mail.com] => 11
[sara@mail.com] => 12
)

Match country codes with currency:

$currency = [
    ["code" => "US", "currency" => "Dollar"],
    ["code" => "UK", "currency" => "Pound"],
    ["code" => "JP", "currency" => "Yen"]
];

$result = array_column($currency, "currency", "code");
print_r($result);

Here is the output:

Array
(
[US] => Dollar
[UK] => Pound
[JP] => Yen
)

This example maps each country code to its currency. The function builds one array where the code is the key and the currency is the value.

Wrapping Up

You learned what array_column does and how it works. You also saw how to use it with simple arrays, associative keys, and duplicate values.

Here is a quick recap:

  • The function extracts one column from a multidimensional array.
  • You can set a key field to use as the index.
  • Duplicate keys replace earlier values.
  • It works with both numeric and associative arrays.

FAQs

What does PHP array_column do?

The array_column function returns the values from a single column in a multi-dimensional array. Example:
$data = [
  ["id" => 1, "name" => "Ali"],
  ["id" => 2, "name" => "Sara"]
];
$result = array_column($data, "name");
print_r($result);

/*
Output:
Array
(
  [0] => Ali
  [1] => Sara
)
*/

How do I use array_column with keys?

You can pass a third parameter to use a column as keys. Example:
$data = [
  ["id" => 1, "name" => "Ali"],
  ["id" => 2, "name" => "Sara"]
];
$result = array_column($data, "name", "id");
print_r($result);

/*
Output:
Array
(
  [1] => Ali
  [2] => Sara
)
*/

Can array_column handle nested arrays?

Yes, array_column works on multi-dimensional arrays and extracts values. Example:
$data = [
  ["id" => 1, "info" => ["city" => "Riyadh"]],
  ["id" => 2, "info" => ["city" => "Jeddah"]]
];
$result = array_column($data, "info");
print_r($result);

/*
Output:
Array
(
  [0] => Array
      (
          [city] => Riyadh
      )

  [1] => Array
      (
          [city] => Jeddah
      )
)
*/

What is the difference between array_column and array_map?

  • array_column extracts values by column name or index.
  • array_map applies a callback to each element.
Example:
$data = [
  ["id" => 1, "name" => "Ali"],
  ["id" => 2, "name" => "Sara"]
];

$col = array_column($data, "name");
$map = array_map(function($v) { return $v["name"]; }, $data);

print_r($col);
print_r($map);

Similar Reads

PHP compact Function: Assoc Array from Variables

The PHP compact function is a way to pull variables into an array. In this article, you will learn how…

PHP MySQL WHERE: How to Filter Data in MySQL

Filtering data is a big part of getting the right information to show up. That is where the WHERE clause…

PHP Spaceship Operator

If you are new to the PHP Spaceship Operator, then let me tell you, you're in for a treat. This…

PHP $_GET: How to Create Dynamic URLs in PHP?

The PHP $_GET— is a tiny part, but strong in the data processing using the URL. There is a site that…

How to Create Table in MySQL with PHP?

One of the first tasks you will likely encounter is creating a table in databases. Tables are the main part…

History of PHP: From PHP/FI to Modern Web Development

You use PHP every day if you build websites, but most people do not know where it came from or…

PHP Ternary Operator: How to Write Short Expressions

The PHP ternary operator gives you a way to write short expressions. It reduces lines of code and avoids long…

PHP Null Coalescing Operator: Handling Null Values

The PHP null coalescing operator, written as ??, is a helpful feature added in PHP 7. It makes it easier…

Check If a Row Exists in PDO

At times, you may need to check if a row exists to avoid duplication when inserting or to prevent issues…

Traits in PHP: What They Are & Examples

PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…

Previous Article

Math Functions in JavaScript with Examples

Next Article

HTML Introduction for Beginners from Scratch

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.