Ta Lha Ta Lha - 30 days ago 16
PHP Question

How to flatten and sort a multi-dimensional array containing numbers and subarrays of numbers?

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];
}

Answer Source

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