Home > Backend Development > PHP Tutorial > How do you flatten multidimensional arrays in PHP into a two-dimensional array with dot notation keys?

How do you flatten multidimensional arrays in PHP into a two-dimensional array with dot notation keys?

Susan Sarandon
Release: 2024-11-01 07:09:02
Original
280 people have browsed it

How do you flatten multidimensional arrays in PHP into a two-dimensional array with dot notation keys?

Converting Multidimensional Arrays to 2D Arrays with Dot Notation Keys in PHP

Multidimensional arrays in PHP provide a convenient way to organize complex data structures. However, sometimes it may become necessary to flatten a multidimensional array into a two-dimensional array with dot notation keys. This can be achieved using a recursive approach.

Consider the following example:

<code class="php">$myArray = [
    'key1' => 'value1',
    'key2' => [
        'subkey' => 'subkeyval'
    ],
    'key3' => 'value3',
    'key4' => [
        'subkey4' => [
            'subsubkey4' => 'subsubkeyval4',
            'subsubkey5' => 'subsubkeyval5',
        ],
        'subkey5' => 'subkeyval5'
    ]
];</code>
Copy after login

To convert this array into a two-dimensional array with dot notation keys, we can use the following code:

<code class="php">$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($myArray));
$result = [];
foreach ($ritit as $leafValue) {
    $keys = [];
    foreach (range(0, $ritit->getDepth()) as $depth) {
        $keys[] = $ritit->getSubIterator($depth)->key();
    }
    $result[join('.', $keys)] = $leafValue;
}</code>
Copy after login

This code iterates recursively over the multidimensional array, using the RecursiveIteratorIterator and RecursiveArrayIterator classes. For each leaf value in the array, it creates a dot notation key by joining the keys of the parent and child arrays. The result is a new two-dimensional array with dot notation keys, as shown below:

<code class="php">[
    'key1' => 'value1',
    'key2.subkey' => 'subkeyval',
    'key3' => 'value3',
    'key4.subkey4.subsubkey4' => 'subsubkeyval4',
    'key4.subkey4.subsubkey5' => 'subsubkeyval5',
    'key4.subkey5' => 'subkeyval5'
]</code>
Copy after login

This technique can be particularly useful when working with data that needs to be converted to a flat structure for processing or display.

The above is the detailed content of How do you flatten multidimensional arrays in PHP into a two-dimensional array with dot notation keys?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template