1

I need help sorting a multi-dimensional array consisting of numeric strings and arrays of numeric strings.

Here is my code:

$array =  array();
$multi= array(
              10,
              9,
              array(5,4),
              array(6,7),
              array(3,2),
              array(8,1)
        );

foreach ($multi as $value) {
    if (is_numeric($value)) {
        array_push($array, $value);
    }
    if (is_array($value)) {
        array_push($array, $value);
    }
}
sort($array);
for ($i=0; $i <count($array) ; $i++) { 
    echo $array[$i];
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
jompom
  • 23
  • 4
  • i have to sort this array. an array which has numeric values and multiple arrays which has also contains numeric value and i want to sort it ascending order.. – jompom Aug 22 '17 at 20:45
  • i just sort this numeric array. my array and array values is in question. did you sort it? – jompom Aug 22 '17 at 20:55
  • bro i just sort the array in ascending order. and output show 1,2,3,4,5,6,7,8,9,10. it's a task. i don't get the logic to how sort it. – jompom Aug 22 '17 at 21:02
  • Possible duplicate of [How to Flatten a Multidimensional Array?](https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array) – mickmackusa Aug 24 '17 at 00:15

2 Answers2

1

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>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • @TaLha with `$flat` you can use a simple foreach loop to iterate the values. I wouldn't have bothered to post an answer if it wasn't a superior way. Alive to Die's answer works the same. – mickmackusa Aug 22 '17 at 23:45
  • @Talha I want to clarify that when you display `$flat` in your loop (after sorting), you should use `foreach()` or put `count()` in the first expression of [for](http://php.net/manual/en/control-structures.for.php), this way you aren't calling `count()` on each iteration. – mickmackusa Aug 23 '17 at 00:41
0

I hope you want like this:-

foreach ($multi as $value) {
    if (is_numeric($value)) {
        $array[] =  $value;
    }if (is_array($value)) {
        foreach($value as $val){
            $array[] =  $val;
        }
    }
}
sort($array);
print_r($array);

Output:-https://eval.in/848749

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98