fork download
  1. <?php
  2.  
  3. $test = array(
  4. 'abc' => 123,
  5. 'def' => 456,
  6. 'test' => array(
  7. 123 => 'hip',
  8. 'hop' => null
  9. )
  10. );
  11.  
  12. var_dump(flatten($test));
  13.  
  14.  
  15.  
  16. function flatten($arr, $prefix = '') {
  17. $out = [];
  18. foreach($arr as $key => $val){
  19. if(is_array($val))
  20. $out = array_merge($out, flatten($val, $prefix . $key . '.'));
  21. else
  22. $out[$prefix . $key] = $val;
  23. }
  24. return $out;
  25. }
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
array(4) {
  ["abc"]=>
  int(123)
  ["def"]=>
  int(456)
  ["test.123"]=>
  string(3) "hip"
  ["test.hop"]=>
  NULL
}