Skip to content

Instantly share code, notes, and snippets.

@oziks
Last active August 3, 2018 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oziks/4770855 to your computer and use it in GitHub Desktop.
Save oziks/4770855 to your computer and use it in GitHub Desktop.
flatten php function via http://ideone.com/1dBqx
<?php
$data = array(
'user' => array(
'email' => 'user@example.com',
'name' => 'Super User',
'address' => array(
'billing' => 'Street 1',
'delivery' => 'Street 2'
)
),
'post' => 'Hello, World!'
);
print_r(flatten($data));
<?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;
}
Array
(
[user.email] => user@example.com
[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