fork(70) download
  1. <?php
  2.  
  3. $data = array(
  4. 'user' => array(
  5. 'email' => 'user@example.com',
  6. 'name' => 'Super User',
  7. 'address' => array(
  8. 'billing' => 'Street 1',
  9. 'delivery' => 'Street 2'
  10. )
  11. ),
  12. 'post' => 'Hello, World!'
  13. );
  14.  
  15. function flatten($array, $prefix = '') {
  16. $result = array();
  17. foreach($array as $key=>$value) {
  18. if(is_array($value)) {
  19. $result = $result + flatten($value, $prefix . $key . '.');
  20. }
  21. else {
  22. $result[$prefix.$key] = $value;
  23. }
  24. }
  25. return $result;
  26. }
  27.  
  28. print_r(flatten($data));
Success #stdin #stdout 0.02s 13112KB
stdin
Standard input is empty
stdout
Array
(
    [user.email] => user@example.com
    [user.name] => Super User
    [user.address.billing] => Street 1
    [user.address.delivery] => Street 2
    [post] => Hello, World!
)