How do I return the ordered value of arsort for variables?

1

Well, my question is how to return the value contained within "arsort ($ array);" in variables that can be used in other parts of the code. but the same ones being sorted as the result that appears in the "var_dump" type: $ 1 = 5, $ 2 = 10, $ 3 = 26, $ 4 = 34. I've tried it several ways, but since I'm new to php, I may be doing it wrong! Thanks in advance for your help!

$num1 = 10;

$num2 = 5;

$num3 = 34;

$num4 = 26;


$array = array($num1, $num2, $num3, $num4);

arsort($array);

var_dump($array);

//Código que aparece quando chamado o var_dump

array(4) {

[2]=> string(1) "5"

[1]=> string(2) "10"

[4]=> string(2) "26" 

[3]=> string(2) "34"

}
    
asked by anonymous 28.04.2016 / 01:59

1 answer

1

You can do this:

$num1 = 10;

$num2 = 5;

$num3 = 34;

$num4 = 26;


$array = array($num1, $num2, $num3, $num4);

$sort = arsort($array);

$nova_array = array();
$i = 0

foreach($sort as $value) {
    $nova_array[$i] = $value;
    $i++;
}

//Agora você pode usar assim:

echo $nova_array[0]; // imprime 5

echo $nova_array[1]; // imprime 10
    
28.04.2016 / 23:24