Advertisement
Guest User

some arrays playground

a guest
Dec 12th, 2014
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. <?php
  2. $twoDimensional = []; // [5][5]
  3.  
  4. // fill [5][5] with rand(1,100)
  5. for ($i = 0; $i < 5; $i++) {
  6.     for ($j = 0; $j < 5; $j++) {
  7.         $twoDimensional[$i][$j] = rand(1,100);
  8.     }
  9. }
  10.  
  11.  
  12. $normal = []; //[25]
  13.  
  14. // fill up to 25 elements between 30 and 70 in $normal
  15. foreach ($twoDimensional as $firstDimension) {
  16.     foreach ($firstDimension as $value) {
  17.         if ($value >= 30 && $value <= 70 && count($normal) <= 25) {
  18.             $normal[] = $value;
  19.         }
  20.     }
  21. }
  22.  
  23. var_dump(array_sum($normal)); // the sum of all elements
  24.  
  25. sort($normal, SORT_DESC);
  26.  
  27. var_dump($normal); // let's see what in the array
  28.  
  29. /**
  30.  *
  31.  array (size=15)
  32.     0 => int 31
  33.     1 => int 32
  34.     2 => int 34
  35.     3 => int 37
  36.     4 => int 40
  37.     5 => int 41
  38.     6 => int 47
  39.     7 => int 59
  40.     8 => int 59
  41.     9 => int 60
  42.     10 => int 60
  43.     11 => int 61
  44.     12 => int 62
  45.     13 => int 64
  46.     14 => int 65
  47.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement