3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function reindexWeeks($sourceWeeks, &$destWeeks, &$idxNew) { foreach($sourceWeeks as $sourceKey => $sourceValue) { if (is_array($sourceValue)) { $destWeeks[$idxNew] = array(); reindexWeeks($sourceValue, $destWeeks[$idxNew], $idxNew); } else { $destWeeks[$idxNew] = $sourceValue; $idxNew++; } } } $idxNew = 1; // starting index. $newWeeks = array(); // output array at the 'week' level $month = array ( 'weeks'=> array( 'w1'=> array( 'd1'=> 'day one', 'd2'=> 'day two', 'd3'=> array( 'H1' => 'hour one', 'H2' => 'hour two' ) ), 'w2'=> array( 'd1'=> 'day one', 'd2'=> 'day two' ) ) ); reindexWeeks($month['weeks'], $newWeeks, $idxNew); // re-index the array print_r($newWeeks);
Output for 4.3.0 - 7.1.0
Array ( [1] => Array ( [1] => day one [2] => day two [3] => Array ( [3] => hour one [4] => hour two ) ) [5] => Array ( [5] => day one [6] => day two ) )