Content · Programming · PHP

PHP array_column() Function: Usage and Examples

Learn how to use PHP array_column() to extract values from multidimensional arrays, create indexed lookup arrays, and organize records with practical examples.

2 views Published Oct 16, 2026 Updated Oct 16, 2026 Admin

When working with multidimensional arrays in PHP, you may need to extract a specific field from every record. For example, you might want to collect customer names, product prices, or employee IDs.

Instead of writing a loop to retrieve each value, PHP provides the array_column() function.

In this guide, you'll learn how to extract array columns, create associative lookup arrays, and handle missing or duplicate keys.

What Is array_column() in PHP?


The array_column() function extracts values from a specific column in a multidimensional array.

Its syntax is:

array_column(
    array $array,
    int|string|null $column_key,
    int|string|null $index_key = null
): array

It accepts three parameters:

  • $array — An array containing records represented by arrays or objects.
  • $column_key — The key or property name whose values you want to extract.
  • $index_key — An optional field used as the keys of the returned array.

The function returns a new array containing the selected values. The original array remains unchanged.

array_column() has been available since PHP 5.5 and is fully supported in PHP 8.

Example 1: Extracting a Single Column


Imagine you have an array containing several products.

<?php

$products = [
    ["id" => 101, "name" => "Keyboard"],
    ["id" => 102, "name" => "Mouse"],
    ["id" => 103, "name" => "Monitor"]
];

$names = array_column($products, "name");

print_r($names);

Output:

Array
(
    [0] => Keyboard
    [1] => Mouse
    [2] => Monitor
)

PHP extracts the name field from every product and returns a new indexed array.

This is useful when you only need one field from a larger collection.

Example 2: Using index_key to Create an Associative Array


The third parameter lets you specify which field should become the key in the resulting array.

Using the same product structure:

<?php

$products = [
    ["id" => 101, "name" => "Keyboard"],
    ["id" => 102, "name" => "Mouse"],
    ["id" => 103, "name" => "Monitor"]
];

$namesById = array_column(
    $products,
    "name",
    "id"
);

print_r($namesById);

Output:

Array
(
    [101] => Keyboard
    [102] => Mouse
    [103] => Monitor
)

The result is an associative array where:

  • Each product ID becomes an array key.
  • Each product name becomes the corresponding value.

You can now retrieve a product name directly:

echo $namesById[102];

Output:

Mouse

This is particularly useful when you need frequent lookups by a unique identifier.

Example 3: Reindexing Complete Records


Sometimes you want to preserve all fields in each record but change how the records are indexed.

You can do this by passing null as the second parameter.

<?php

$users = [
    ["id" => 10, "name" => "Alex"],
    ["id" => 20, "name" => "Taylor"]
];

$usersById = array_column(
    $users,
    null,
    "id"
);

print_r($usersById);

Output:

Array
(
    [10] => Array
        (
            [id] => 10
            [name] => Alex
        )

    [20] => Array
        (
            [id] => 20
            [name] => Taylor
        )
)

Each user is now accessible by ID without searching through the entire original array.

For example, $usersById[20]["name"] returns Taylor.

Practical Example: Building an Inventory Lookup


Let's build a small inventory example that retrieves product stock levels using product IDs.

Create a file named inventory.php:

<?php

declare(strict_types=1);

$products = [
    [
        "id" => 101,
        "name" => "Keyboard",
        "stock" => 12
    ],
    [
        "id" => 102,
        "name" => "Mouse",
        "stock" => 0
    ],
    [
        "id" => 103,
        "name" => "Monitor",
        "stock" => 5
    ]
];

// Create lookups indexed by product ID.

$names = array_column($products, "name", "id");
$stock = array_column($products, "stock", "id");

// Check a specific product.

$productId = 102;

if (
    isset($names[$productId]) &&
    isset($stock[$productId])
) {
    $message = $names[$productId]
        . ": "
        . ($stock[$productId] > 0
            ? $stock[$productId] . " available"
            : "Out of stock");

    echo htmlspecialchars(
        $message,
        ENT_QUOTES,
        "UTF-8"
    );
} else {
    echo "Product not found.";
}

Output:

Mouse: Out of stock

The code creates two lookup arrays using array_column().

Instead of looping through the products every time, the application retrieves the name and stock directly by product ID.

This approach is useful for inventory checks, report generation, and mapping related data. It assumes the input records contain valid, unique product IDs.

What Happens with Missing or Duplicate Keys?


There are a few important behaviors to understand:

Situation Behavior
A record is missing the requested column That record is skipped.
A record is missing the index key PHP assigns an automatically generated numeric key.
Multiple records produce the same index key The later value replaces the earlier one.
column_key is null The complete records are returned.
The input array is empty An empty array is returned.

For example, if two products share the same ID, using that ID as index_key may cause one product to overwrite the other.

Important: When every record must be preserved, verify that the required columns exist and that index values are unique before using the result.

Can array_column() Work with Objects?


Yes. PHP supports extracting values from arrays of objects as well as multidimensional arrays.

For objects, array_column() can directly access public properties.

<?php

$users = [
    (object) ["name" => "Alex"],
    (object) ["name" => "Taylor"]
];

$names = array_column($users, "name");

print_r($names);

Output:

Array
(
    [0] => Alex
    [1] => Taylor
)

Private and protected properties are not directly accessible. Accessing them requires appropriate magic methods, so don't assume that every property in an object can be extracted automatically.

array_column() vs foreach


Both approaches can extract information from a collection.

Use array_column() when you need a straightforward extraction of one field or a simple lookup array.

Use foreach when your processing requires calculations, complex conditions, transformations, or special handling of missing values.

Neither approach is universally faster in every situation. Choose based on readability and actual performance requirements.

Key Takeaways


  • array_column() extracts values from a specific field in an array of records.
  • The second parameter identifies the column to retrieve.
  • The optional third parameter determines the keys of the result.
  • Use null as the column key to reindex complete records.
  • Missing columns are skipped, while duplicate index keys can overwrite values.
  • The function can also extract accessible properties from objects.
  • It's particularly useful for creating associative lookup arrays.

The array_column() function provides a simple and readable way to extract structured data in PHP. Whether you're collecting field values or building quick lookups, it can eliminate unnecessary loops and keep your code easier to maintain.

For additional details, see the official PHP array_column() documentation.

Share this resourceSend this page to someone who may find it useful.