How to sort numbers in an array?

4

I do not find the function to sort the numbers drawn more and more.

<?php
   $resultado = array();
   for ($i = 0; $i <= 5; $i++){
        array_push($resultado,rand(1,60));
   }

   print_r($resultado); # Exibe os números sorteados fora de ordem.
   # print_r(sort($resultado)) # Exibe apenas o número 1
    
asked by anonymous 06.09.2015 / 12:11

2 answers

4

You know how to do it, just do not know how to see the result. The sort() function performs what you want on top of the array that you pass. Then you just print the array again after going through the function that everything will work out. You just can not print the function return because according to the documentation (you have to read it to learn, alias has to read everything to program, to use new sites, etc., read is the secret to evolve), it returns a booliano if it worked or failed. It does not return another sorted array.

<?php
$resultado = array();
for ($i = 0; $i <= 5; $i++) {
    array_push($resultado, rand(1, 60));
}
    print_r($resultado);
    sort($resultado);
    print_r($resultado);

See running on ideone .

    
06.09.2015 / 12:33
3

The function sort($resultado) does not return the ordered array but a Boolean if it runs without errors, or with errors ..

The correct way to use it is:

sort($resultado);

and then, print_r($resultado) .

That is:

$resultado = array();
for ($i = 0; $i <= 5; $i++){
    array_push($resultado, rand(1,60));
}

sort($resultado);
print_r($resultado);

example: link

    
06.09.2015 / 12:34