//since this is recursive we need to have the kill condition if(empty($array)){ //we have an empty array, return an empty array return $resultArray; }else{ //here we need to deal with the array kind of like a stack. //first we will check if the current 0 indexed item is an array if(is_array($array[0])){ $resultArray = collapseArray($array[0]); }else{ $resultArray[] = $array[0]; }

//now we pop the saved element off and call the function again $array = array_reverse($array); array_pop($array); $array = array_reverse($array);

$returnedArray = collapseArray($array);

//merge the recent element with the returned array $finalArray = array_merge($resultArray,$returnedArray);