Current Location: Home> Latest Articles> How to Convert a Multidimensional Array to a One-Dimensional Array in PHP

How to Convert a Multidimensional Array to a One-Dimensional Array in PHP

gitbox 2025-06-30

Introduction to Converting Multidimensional Arrays to One-Dimensional Arrays in PHP

In PHP development, it's common to encounter the need to convert multidimensional arrays into one-dimensional arrays. A multidimensional array is an array that contains other arrays as its elements, while a one-dimensional array consists of simple data types as elements. This article will introduce two common methods for converting multidimensional arrays into one-dimensional arrays.

Converting a Multidimensional Array to a One-Dimensional Array Using Recursion

Recursion is a function that calls itself and is useful for handling arrays with multiple layers of nested elements. Here is an example of how to convert a multidimensional array into a one-dimensional array using recursion:

function flattenArray($array, &$result = array()) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            flattenArray($value, $result);
        } else {
            $result[$key] = $value;
        }
    }
    return $result;
}

// Example Usage
$multiDimensionalArray = array(
    'a' => 1,
    'b' => array(
        'c' => 2,
        'd' => 3
    ),
    'e' => 4
);
$flattenArray = flattenArray($multiDimensionalArray);
print_r($flattenArray);

In the example above, we define a recursive function flattenArray that takes a multidimensional array and an empty result array as parameters. It loops through the array, checking if each element is an array. If it is, the function calls itself recursively to flatten that array. If it's a basic data type, it adds the element to the result array.

Converting Using Reference Assignment

Another way to convert a multidimensional array into a one-dimensional array in PHP is by using reference assignment combined with iterators. Here is an example:

function flattenArray($array) {
    $result = array();
    $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
    foreach ($iterator as $value) {
        $keys = array();
        foreach (range(0, $iterator->getDepth()) as $depth) {
            $keys[] = $iterator->getSubIterator($depth)->key();
        }
        $result[implode('.', $keys)] = $value;
    }
    return $result;
}

// Example Usage
$multiDimensionalArray = array(
    'a' => 1,
    'b' => array(
        'c' => 2,
        'd' => 3
    ),
    'e' => 4
);
$flattenArray = flattenArray($multiDimensionalArray);
print_r($flattenArray);

In this example, we use PHP's recursive iterators RecursiveIteratorIterator and RecursiveArrayIterator to flatten the array. These iterators allow us to get the key path for each element, which we then use as the key for the one-dimensional result array.

Conclusion

This article introduced two common methods for converting multidimensional arrays into one-dimensional arrays: using a recursive function and using reference assignment with recursive iterators. The recursive function approach is simple and easy to understand, suitable for handling arrays with fewer layers of nesting. The reference assignment method, combined with iterators, offers more flexibility, making it ideal for more complex nested arrays. Depending on the specific development needs, choosing the appropriate method can enhance both the efficiency and readability of your code.