Skip to content

Instantly share code, notes, and snippets.

@polonskiy
Forked from oziks/demo.php
Created September 2, 2013 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save polonskiy/6412223 to your computer and use it in GitHub Desktop.
Save polonskiy/6412223 to your computer and use it in GitHub Desktop.
<?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;
}
<?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));
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