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' );
Dot implements PHP's ArrayAccess interface and Dot object can also be used the same way as normal arrays with additional dot notation.
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.
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.
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 ) );
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.
) 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.
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...), ... )
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With