Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Convert multidimensional array to 2D array with dot notation keys

Tags:

arrays

php

There are plenty of tips and code examples out there of accessing PHP arrays with dot notation, but I would like to do somewhat the opposite. I would like to take a multidimensional array like this:

$myArray = array(     'key1' => 'value1',     'key2' => array(         'subkey' => 'subkeyval'     ),     'key3' => 'value3',     'key4' => array(         'subkey4' => array(             'subsubkey4' => 'subsubkeyval4',             'subsubkey5' => 'subsubkeyval5',         ),         'subkey5' => 'subkeyval5'     ) ); 

And turn it into this (likely through some recursive function):

$newArray = array(     'key1'                    => 'value1',     'key2.subkey'             => 'subkeyval',     'key3'                    => 'value3',     'key4.subkey4.subsubkey4' => 'subsubkeyval4',     'key4.subkey5.subsubkey5' => 'subsubkeyval5',     'key4.subkey5'            => 'subkeyval5' ); 
like image 835
TheCheese Avatar asked May 03 '12 02:05

TheCheese


People also ask

Does PHP have dot notation?

Dot implements PHP's ArrayAccess interface and Dot object can also be used the same way as normal arrays with additional dot notation.

How do I flatten a multidimensional array in PHP?

Use the array_walk_recursive to Flatten a Multidimensional Array in PHP. The built-in function array_walk_recursive can be used with a closure function to flatten a multidimensional array in PHP.

Is multidimensional array PHP?

A multidimensional array is an array containing one or more arrays. PHP supports multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

How can you create a multidimensional array in PHP?

You create a multidimensional array using the array() construct, much like creating a regular array. The difference is that each element in the array you create is itself an array. For example: $myArray = array( array( value1 , value2 , value3 ), array( value4 , value5 , value6 ), array( value7 , value8 , value9 ) );

What is a 2D array in PHP?

Introduction to 2D Arrays in PHP An array is a collection of elements of any datatype. There are many data types in php like string, integer, boolean, array, object, resource…etc. A 2D array is a mix of these data types mainly the array.

How to access multidimensional array elements in PHP?

) Accessing multidimensional array elements: There are mainly two ways to access multidimensional array elements in PHP. Elements can be accessed using dimensions as array_name [‘first dimension’] [‘second dimension’]. Elements can be accessed using for loop.

What is a two dimensional array in JavaScript?

Two dimensional array: It is the simplest form of a multidimensional array. It can be created using nested array. These type of arrays can be used to store any type of elements, but the index is always a number. By default, the index starts with zero. array ( array (elements...), array (elements...), ... )

What are multi-dimensional arrays?

Multi-dimensional arrays are such type of arrays which stores an another array at each index instead of single element. In other words, define multi-dimensional arrays as array of arrays.


1 Answers

teh codez

$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($myArray)); $result = array(); foreach ($ritit as $leafValue) {     $keys = array();     foreach (range(0, $ritit->getDepth()) as $depth) {         $keys[] = $ritit->getSubIterator($depth)->key();     }     $result[ join('.', $keys) ] = $leafValue; } 

output

Array (     [key1] => value1     [key2.subkey] => subkeyval     [key3] => value3     [key4.subkey4.subsubkey4] => subsubkeyval4     [key4.subkey4.subsubkey5] => subsubkeyval5     [key4.subkey5] => subkeyval5 ) 

demo: http://codepad.org/YiygqxTM

I need to go, but if you need an explanation of that tomorrow, ask me.

like image 131
goat Avatar answered Oct 03 '22 02:10

goat