3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function flatten_array($array, $flat = array()) { foreach($array as $k=>$v){ $k = strval($k); if(!is_array($v)){ $i = 0; while(true){ $i++; $key = $k."-".strval($i); if(!isset($flat[$key])) break; } $flat[$key] = $v; }else{ $flat = flatten_array($v, $flat); } } return $flat; } // this line generates a 10x10 multi-d array $a = array_fill(0, 10, array_fill(0, 10, 0)); $flat_array = flatten_array($a); var_dump($flat_array);
Output for 4.3.0 - 7.2.0
array(100) { ["0-1"]=> int(0) ["1-1"]=> int(0) ["2-1"]=> int(0) ["3-1"]=> int(0) ["4-1"]=> int(0) ["5-1"]=> int(0) ["6-1"]=> int(0) ["7-1"]=> int(0) ["8-1"]=> int(0) ["9-1"]=> int(0) ["0-2"]=> int(0) ["1-2"]=> int(0) ["2-2"]=> int(0) ["3-2"]=> int(0) ["4-2"]=> int(0) ["5-2"]=> int(0) ["6-2"]=> int(0) ["7-2"]=> int(0) ["8-2"]=> int(0) ["9-2"]=> int(0) ["0-3"]=> int(0) ["1-3"]=> int(0) ["2-3"]=> int(0) ["3-3"]=> int(0) ["4-3"]=> int(0) ["5-3"]=> int(0) ["6-3"]=> int(0) ["7-3"]=> int(0) ["8-3"]=> int(0) ["9-3"]=> int(0) ["0-4"]=> int(0) ["1-4"]=> int(0) ["2-4"]=> int(0) ["3-4"]=> int(0) ["4-4"]=> int(0) ["5-4"]=> int(0) ["6-4"]=> int(0) ["7-4"]=> int(0) ["8-4"]=> int(0) ["9-4"]=> int(0) ["0-5"]=> int(0) ["1-5"]=> int(0) ["2-5"]=> int(0) ["3-5"]=> int(0) ["4-5"]=> int(0) ["5-5"]=> int(0) ["6-5"]=> int(0) ["7-5"]=> int(0) ["8-5"]=> int(0) ["9-5"]=> int(0) ["0-6"]=> int(0) ["1-6"]=> int(0) ["2-6"]=> int(0) ["3-6"]=> int(0) ["4-6"]=> int(0) ["5-6"]=> int(0) ["6-6"]=> int(0) ["7-6"]=> int(0) ["8-6"]=> int(0) ["9-6"]=> int(0) ["0-7"]=> int(0) ["1-7"]=> int(0) ["2-7"]=> int(0) ["3-7"]=> int(0) ["4-7"]=> int(0) ["5-7"]=> int(0) ["6-7"]=> int(0) ["7-7"]=> int(0) ["8-7"]=> int(0) ["9-7"]=> int(0) ["0-8"]=> int(0) ["1-8"]=> int(0) ["2-8"]=> int(0) ["3-8"]=> int(0) ["4-8"]=> int(0) ["5-8"]=> int(0) ["6-8"]=> int(0) ["7-8"]=> int(0) ["8-8"]=> int(0) ["9-8"]=> int(0) ["0-9"]=> int(0) ["1-9"]=> int(0) ["2-9"]=> int(0) ["3-9"]=> int(0) ["4-9"]=> int(0) ["5-9"]=> int(0) ["6-9"]=> int(0) ["7-9"]=> int(0) ["8-9"]=> int(0) ["9-9"]=> int(0) ["0-10"]=> int(0) ["1-10"]=> int(0) ["2-10"]=> int(0) ["3-10"]=> int(0) ["4-10"]=> int(0) ["5-10"]=> int(0) ["6-10"]=> int(0) ["7-10"]=> int(0) ["8-10"]=> int(0) ["9-10"]=> int(0) }