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.
Table of Content
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" => "[email protected]"],
["id" => 22, "email" => "[email protected]"],
["id" => 33, "email" => "[email protected]"]
];
$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] => [email protected]
[1] => [email protected]
[2] => [email protected]
)
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" => "[email protected]"],
["id" => 12, "email" => "[email protected]"]
];
$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
(
[[email protected]] => 11
[[email protected]] => 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?
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?
$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?
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_columnextracts values by column name or index.array_mapapplies a callback to each element.
$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
The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…
The NOT operator in PHP (!) provides the reverse of whatever truth value is given by a certain expression. It’s…
PHP developers may be need to determine the number of elements in an array. The count() function in PHP makes…
The PHP $_GET— is a tiny part, but strong in the data processing using the URL. There is a site that…
PHP array_diff_key compares arrays and removes elements that share the same key. It checks only keys and keeps the values…
Traditional relational databases like MySQL often take center stage. However, with the rise of NoSQL databases, MongoDB has emerged as…
In PHP, manipulation of XML data can be very critical when most systems' data exchange relies on the format. XML—Extensible…
You use PHP every day if you build websites, but most people do not know where it came from or…
The array_intersect_key in PHP compares arrays by keys and returns only the parts that match. It is useful when you…
The filter_var_array() appeared to make input validation simple and sanitization in PHP. It handles multiple inputs with different filters was…