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 $GLOBALS: Access Global Variables with Examples

When you start working with PHP, you’ll soon need access to variables from multiple places in your code. For this,…

PHP Conditional Operator: How It Works with Examples

The PHP shorthand conditional operator gives a quick way to choose between two values and replaces long if-else blocks. What…

PHP strtoupper Function: Convert Strings to Uppercase

Use strtoupper() function when you want to change all letters in a string to uppercase in PHP. It works with…

PHP continue Statement: How to Skip Steps in Iteration

The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…

PHP str_repeat: How it Works with Examples

PHP gives you many string tools. One of them is php str_repeat. This function repeats a texts as many times…

PHP array_find_key: How it Works with Examples

The PHP array_find_key function finds a key in an array by a condition. It checks each element and returns the…

PHP Array: Accessing and Managing Elements

PHP arrays are lists or maps that contain multiple values grouped by array keys. They may include integers, strings, booleans,…

PHP Object | How to Create an Instance of a Class

The PHP object is an instance from the PHP class or the main data structure that is already been created…

PHP Static Property: How It Works & Examples

PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…

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…

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.