Array flatten with recursion
PHP already has a native function for this task.

array_walk_recursive() traverses your "leaf nodes" for you in one step:

Code: (Demo)

//$multi=[1,2,[3,[4]],5];
$multi=[array('A', 
        array('B',
            array('CC','CCCC','CCCCCC'), 
            array('DDD','EFG','HIJ','LMNOP')
        )
    ), 
    array('2'), 
    'b',
    array(array(),array(),array())];
array_walk_recursive($multi,function($v)use(&amp;$flat){$flat[]=$v;});
var_export($flat);


Output:

array (
  0 =&gt; 'A',
  1 =&gt; 'B',
  2 =&gt; 'CC',
  3 =&gt; 'CCCC',
  4 =&gt; 'CCCCCC',
  5 =&gt; 'DDD',
  6 =&gt; 'EFG',
  7 =&gt; 'HIJ',
  8 =&gt; 'LMNOP',
  9 =&gt; '2',
  10 =&gt; 'b',
)