Last active
August 3, 2018 10:10
-
-
Save oziks/4770855 to your computer and use it in GitHub Desktop.
flatten php function via http://ideone.com/1dBqx
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 | |
$data = array( | |
'user' => array( | |
'email' => '[email protected]', | |
'name' => 'Super User', | |
'address' => array( | |
'billing' => 'Street 1', | |
'delivery' => 'Street 2' | |
) | |
), | |
'post' => 'Hello, World!' | |
); | |
print_r(flatten($data)); |
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 | |
function flatten($array, $prefix = '') { | |
$result = array(); | |
foreach($array as $key=>$value) { | |
if(is_array($value)) { | |
$result = $result + flatten($value, $prefix . $key . '.'); | |
} | |
else { | |
$result[$prefix.$key] = $value; | |
} | |
} | |
return $result; | |
} |
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
Array | |
( | |
[user.email] => [email protected] | |
[user.name] => Super User | |
[user.address.billing] => Street 1 | |
[user.address.delivery] => Street 2 | |
[post] => Hello, World! | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment