Convert multidimensional array into single array

Convert multidimensional array into single array

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

Stefan Bogdanescu

Founder & Senior Architect

Flattening Nested Arrays: Extracting Values by Key

You are looking at a classic data normalization problem where your current structure nests every item in an unnecessary wrapper. The array you have right now looks like a list of single-item arrays, each containing a plan key. This redundancy often happens when data is aggregated from a database query that returns rows as objects, or perhaps after iterating through a collection and pushing into a new array without cleaning up the structure.

Your goal is to strip away that nesting layer and retrieve just the values associated with the plan key. In PHP, this is a straightforward operation, but understanding why you choose one method over another matters for performance and code clarity.

The most direct approach uses the array_column function. This built-in utility was specifically designed to extract a column of data from a multidimensional array, treating the first dimension as rows and the second as columns. Given your input structure, where every sub-array has exactly one key named plan, array_column is the idiomatic solution.

Here is how you implement it:

$plans = [
    ['plan' => 'basic'],
    ['plan' => 'small'],
    ['plan' => 'novice'],
    ['plan' => 'professional'],
    ['plan' => 'master'],
    ['plan' => 'promo'],
    ['plan' => 'newplan'],
];

$flattened = array_column($plans, 'plan');

This one-liner returns exactly the flat array you need. It iterates over the outer array, accesses the plan key in each sub-array, and collects them into a new indexed list. The resulting array preserves the original order, which is crucial if you intend to map these values to UI elements or pass them to an API that expects specific sequencing.

If you are working within the Laravel ecosystem, this pattern is even more common when handling Eloquent collections. You might encounter a situation where ->pluck() feels more appropriate than array_column because it is a chainable method on the Collection class. If your data comes from a database query like $plans = Plan::all();, and you want to extract just the plan names, you can do:

$planNames = $plans->pluck('plan')->toArray();

This achieves the same result but keeps you within Laravel's collection syntax, making the intent explicit to other developers reading the code.

However, array_column is not without limitations. It expects every sub-array to contain the specified key. If any item in your nested array is missing the plan key or contains null, that value will appear as null in the resulting flat array. This can break logic downstream if you assume every element holds a string. You should always validate your data structure before flattening if the source is untrusted or dynamic.

Another potential pitfall arises when dealing with keys that aren't integers. If your outer array uses associative keys (strings) instead of numeric indices, array_column will still work, but the resulting array might not be strictly indexed numerically starting from zero unless you explicitly re-index it using array_values. In many cases, this automatic re-indexing is desirable, as it prevents gaps in the array that could confuse loops or database insertions.

Performance-wise, array_column is highly optimized because it avoids creating intermediate objects during the iteration process. It operates at C-level speed within the PHP interpreter, making it significantly faster than a manual foreach loop for large datasets. While a manual loop might look like this:

$flattened = [];
foreach ($plans as $item) {
    $flattened[] = $item['plan'];
}

It is functionally identical in logic but slower at scale and less readable. Unless you need to perform additional processing on each item during the iteration, prefer array_column for its conciseness and speed.

Our network