Good day, dear experts.

You must write a function that creates an array of random numbers and returns in a sorted order in descending order.

Attention a question: How to deduce all values ​​from an array?

function massive($m) { $mass = []; $znach = mt_rand(10, 20); for($i=0; $i<$m; $i++) { $mass[] = mt_rand(1, 30); } rsort($mass); foreach($mass as $key=> $value) { return $value."<br>" } return; } echo massive(6)."\n"; 

    3 answers 3

    Of course , no rows from the function need be returned. To output all array values ​​from a function, the function must return ... an array! And then this array can be output in any of the millions of available methods. Including print_r, implode and the most flexible foreach , which is the standard answer to the question "how to get all values ​​from an array" :

     function massive($m) { $mass = []; for($i=0; $i<$m; $i++) { $mass[] = mt_rand(1, 30); } rsort($mass); return $mass; } $array = massive(6); echo implode(', ', $array), "<br>\n"; echo "<pre>"; print_r($array); echo "</pre>"; foreach($array as $item) { echo "$item<br>\n"; } 

      For this, it is convenient to use the implode() function, which allows you to convert an array into a string with a separator specified in the first argument

       function massive($m) { $mass = []; $znach = mt_rand(10, 20); for($i=0; $i<$m; $i++) { $mass[] = mt_rand(1, 30); } rsort($mass); return implode(', ', $mass) } echo massive(6)."\n"; 
      • Thank you Thanks to your editing of the question, I still learned how to properly formulate the question :) - Demiteli
      • one
        No need to teach beginners to return an array from a function as a string. @Demiteli, do not use this answer. - Ipatiev
      • Do not learn to learn to answer questions and use the answers. - cheops

      As an addition to the previous answer, just use print_r which will output the entire array.

       function massive($m) { $mass = []; $znach = mt_rand(10, 20); for($i=0; $i<$m; $i++) { $mass[] = mt_rand(1, 30); } rsort($mass); return $mass; } print_r(massive(6))."\n";