How can I create an array from the values of another array's key?

How can I create an array from the values of another array's key?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Specific Keys from Multi-Dimensional Arrays in PHP

When working with nested data structures, you often need to flatten a specific dimension of your dataset. Consider the scenario where you have an array of associative arrays, and you want to isolate just one property across all elements. In your specific case, you are starting with $arr1, which holds user information like name and age, but you only care about the name field:

$arr1 = [
    0 => ['name' => 'tom', 'age' => 22],
    1 => ['name' => 'nick', 'age' => 18]
];

Your goal is to produce $arr2, containing only ['tom', 'nick']. While array_filter() is a powerful tool for removing items based on conditions, it operates on the elements themselves, not their internal keys. Using array_filter here would require a callback that returns true/false, which doesn't inherently extract values; it just keeps or discards rows.

The most idiomatic and efficient solution in modern PHP is array_column(). This built-in function was designed specifically for this exact use case: extracting a single column from a multi-dimensional array. You pass the original array as the first argument, followed by the index of the columns you want to extract.

Here is how you implement it:

$arr2 = array_column($arr1, 'name');

This function iterates through $arr1, grabs the value associated with the key name in each sub-array, and appends it to a new indexed array. The result is exactly what you need. It handles the mapping from the nested associative keys back to a simple indexed list automatically.

Why array_column() is Preferable

Before PHP 5.5, developers might have reached for array_map(). While array_map() works here, it requires defining an anonymous function (or a named closure) to extract the key:

// Less readable than array_column
$arr2 = array_map(function($item) {
    return $item['name'];
}, $arr1);

array_column() is more concise and explicitly communicates intent. It tells the reader, "I am pulling a column," rather than "I am mapping over this list." Furthermore, array_column() is slightly faster because it is implemented in C within the Zend Engine, whereas array_map() with a closure adds overhead due to PHP opcode generation for the callback execution.

Handling Missing Keys or Null Values

A critical trade-off to consider with array_column() is how it handles missing keys. If you try to extract a key that doesn't exist in one of the sub-arrays, the function will return null for that position.

For example:

$badData = [
    ['name' => 'tom'], 
    ['age' => 22] // missing 'name' key
];

$result = array_column($badData, 'name');
// $result will be: [0 => 'tom', 1 => null]

In production code, this might break downstream logic if your application expects strings and receives null. You should validate your data structure before running the extraction, or filter out rows where the key is missing. If you need to strip those nulls immediately after extraction, you can chain a filter:

$arr2 = array_column($arr1, 'name');
$arr2 = array_filter($arr2); // Removes all NULL entries

Reusing the Data Source

If your source data is large and stored in a database or fetched from an API, consider whether you need to keep the original structure intact. array_column() creates a new array and leaves $arr1 untouched. This is generally safer than modifying the source directly, especially if other parts of your application rely on $arr1 having both name and age.

Laravel Context

If you are building this functionality within a Laravel project, the underlying logic remains the same because Laravel uses standard PHP functions. However, when dealing with Eloquent models or collections, you might find yourself needing to extract attributes from model instances rather than raw arrays. In those cases, you would typically access the get method of your collection or use specific query scopes to select only the columns you need before converting them to an array.

If you are looking for more advanced patterns regarding data transformation in Laravel, such as mapping Eloquent collections to DTOs (Data Transfer Objects), resources provide a robust way to handle this without exposing internal model properties. For instance, you could define a resource that explicitly maps the name attribute, ensuring your API responses remain consistent regardless of changes to your underlying model structure.

Ultimately, for pure PHP arrays like $arr1, stick with array_column(). It is the standard tool for this job, offering clarity and performance without unnecessary complexity.

Our network