Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $twoDimensional = []; // [5][5]
- // fill [5][5] with rand(1,100)
- for ($i = 0; $i < 5; $i++) {
- for ($j = 0; $j < 5; $j++) {
- $twoDimensional[$i][$j] = rand(1,100);
- }
- }
- $normal = []; //[25]
- // fill up to 25 elements between 30 and 70 in $normal
- foreach ($twoDimensional as $firstDimension) {
- foreach ($firstDimension as $value) {
- if ($value >= 30 && $value <= 70 && count($normal) <= 25) {
- $normal[] = $value;
- }
- }
- }
- var_dump(array_sum($normal)); // the sum of all elements
- sort($normal, SORT_DESC);
- var_dump($normal); // let's see what in the array
- /**
- *
- array (size=15)
- 0 => int 31
- 1 => int 32
- 2 => int 34
- 3 => int 37
- 4 => int 40
- 5 => int 41
- 6 => int 47
- 7 => int 59
- 8 => int 59
- 9 => int 60
- 10 => int 60
- 11 => int 61
- 12 => int 62
- 13 => int 64
- 14 => int 65
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement