This is a MUCH cleaner way with no conditionals and no nested foreach loops. array_walk_recursive()
only interates the "leaf-nodes" so you don't need to check if something is an array and run an inner foreach loop.
Code: (Demo)
$multi=[10,9,[5,4],[6,7],[3,2],[8,1]];
array_walk_recursive($multi,function($v)use(&$flat){$flat[]=$v;});
sort($flat);
var_export($flat);
Output:
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
7 => 8,
8 => 9,
9 => 10,
)
Judging by your earlier closed question, you will want to use this complete method:
Code: (Demo)
$multi=[10,9,[5,4],[6,7],[3,2],[8,1]]; // declare multi-dim array
array_walk_recursive($multi,function($v)use(&$flat){$flat[]=$v;}); // store values
sort($flat); // sort
echo '<center>',implode('</center><br><center>',$flat),'</center>'; // display
// this will generate a trailing <br> tag that you may not wish to have:
/*
foreach($flat as $v){
echo "<center>$v</center><br>";
}
*/
Unrendered HTML Output:
<center>1</center><br>
<center>2</center><br>
<center>3</center><br>
<center>4</center><br>
<center>5</center><br>
<center>6</center><br>
<center>7</center><br>
<center>8</center><br>
<center>9</center><br>
<center>10</center>