Skip to content

Instantly share code, notes, and snippets.

@SeanCannon
Last active June 23, 2025 22:16
Show Gist options
  • Save SeanCannon/6585889 to your computer and use it in GitHub Desktop.
Save SeanCannon/6585889 to your computer and use it in GitHub Desktop.
PHP array_flatten() function. Convert a multi-dimensional array into a single-dimensional array.
<?php
/**
* Convert a multi-dimensional array into a single-dimensional array.
* @author Sean Cannon, LitmusBox.com | [email protected]
* @param array $array The multi-dimensional array.
* @return array
*/
function array_flatten($array) {
if (!is_array($array)) {
return false;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
} else {
$result = array_merge($result, array($key => $value));
}
}
return $result;
}
@brandonmcconnell
Copy link

@sayhicoelho Have you tried my approach above? It should work with associative and non-associative arrays, and be compatible with PHP 7+.

It also supports a depth parameter, so you can easily flatten only the top layer, the first N layers, or infinitely.

@graceman9
Copy link

@brandonmcconnell Using my sample data it gives not what I expected.
By the way, how to use it with unlimited $depth? array_flatten($r, 999) doesn't look good for me
Despite of that your function does the job of flattening, no doubt :) maybe flatten is not the right word because of ambiguity?

@sayhicoelho thanks for 7.x support!

echo json_encode(array_flatten($r, 9));
{
    "a": "value1",
    "c": "value1.1",
    "d": "value1.2",
    "e": "value2",
    "k": "value2.1",
    "m": "value2.2",
    "n": "value2.3",
    "o": "value3",
    "0": "first",
    "1": "second",
    "q": "value5"
}

@brandonmcconnell
Copy link

@graceman9 What data produces an unexpected result?

For infinite depth, just use -1

@graceman9
Copy link

@brandonmcconnell Have you noticed the difference?

{
    "a": "value1",
    "b_c": "value1.1",
}

and

{
    "a": "value1",
    "c": "value1.1",
}

@brandonmcconnell
Copy link

@graceman9 Oh you want the keys to be concatenated, so every key keeps the log of its original path. Yeah, you are correct that my version does not do that. I tried to follow the approach common in other languages. 🙂

@boedy
Copy link

boedy commented Apr 26, 2024

For a simple 2D array:

$multiDimArray = [[1, 2], [3, 4], [5, 6]];
$flatArray = array_reduce($multiDimArray, 'array_merge', []);

@thirdwheel
Copy link

Using variadic: $result = array_merge(...$array);

As was previously stated, this doesn't work for associative arrays, but if you really don't care about the keys, you can try this:

array_merge(...array_values($array))

Tested on PHP 8.2.8

@skbl9991
Copy link

for my task i was needed function for flatten multidimensional array but i also needed keep keys like values, so i add little changes for initial answer and get what i needed

function array_flatten($array) { 
  if (!is_array($array)) { 
    return false; 
  } 
  $result = array(); 
  foreach ($array as $key => $value) { 
    if (is_array($value)) { 
        //just add [$key]
      $result = array_merge($result, [$key], array_flatten($value)); 
    } else { 
      $result = array_merge($result, array($key => $value));
    } 
  } 
  return $result; 
}

$etalonMap = [
    'OneItem' => [
         'item_1' => [
            'item_1_1',
            'item_1_2',
            'item_1_3'
        ],
        'item_2',
        'item_3',
        'item_4',
        'item_5',
    ],
    'TwoItem'=> [
        'TwoItem_1',
    ]
];
print_r(array_flatten($etalonMap));
/**
 * Array
(
    [0] => OneItem
    [1] => item_1
    [2] => item_1_1
    [3] => item_1_2
    [4] => item_1_3
    [5] => item_2
    [6] => item_3
    [7] => item_4
    [8] => item_5
    [9] => TwoItem
    [10] => TwoItem_1
)
**/


``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment