Created
June 2, 2010 08:28
-
-
Save stubbetje/422106 to your computer and use it in GitHub Desktop.
flatten an multidimensional array and combine keys with separator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$flatten = function( $array , $keySeparator = '.' ) use ( & $flatten ) | |
{ | |
if( is_array( $array ) ) { | |
foreach( $array as $name => $value ) { | |
$f = $flatten( $value , $keySeparator ); | |
if( is_array( $f ) ) { | |
foreach( $f as $key => $val ) { | |
$array[ $name . $keySeparator . $key ] = $val; | |
} | |
unset( $array[ $name ] ); | |
} | |
} | |
} | |
return $array; | |
}; |
tkthundr
commented
Jan 16, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment