all in order: №1

$arr1 = []; for ($i=0; $i<10; $i++){ $arr1[$i] = mt_rand(0,10); } 

random 10 arrays are created here with random values.

  $arr2=[]; for($i=0; $i<10; $i++) { $arr2[$i]= []; for($j=0; $j<$arr1[$i]; $j++){ $arr2[$i][$j]=mt_rand(0, 10); } } 

here arrays are created from the elements of the first array further: №3

  $max=$min = $arr2[0]; for ($i=0; $i<10; $i++){ if($max < $arr2[$i]){ $max = $arr2[$i]; } if ($min > $arr2[$i]){ $min = $arr2[$i]; } } print_r($max); print_r($min); 

I find the maximum and minimum values ​​from array No. 2: here is the output from two arrays:

Array (

[0] => 2

[1] => 9

[2] => 6

[3] => 5

[4] => 1

[5] => 9

[6] => 5

[7] => 2

[8] => 5

)

Array

(

[0] => 0

[1] => 1

now you need to add all the numbers from the second array min:

  $min = $arr2[0]; $sum = 0; for ($i=0; $i<10; $i++){ if($min > $arr2[$i]){ $min = $arr2[$i]; } #$sum += $arr2[$i]; } #echo $sum; 

! ) Fatal error: Uncaught Error: Unsupported operand types

tell me how to correctly calculate the elements of this array

  • It seems to me that you have shoals there already in step 3. But in any case, if you need to add the numbers, then why are you doing some checks and comparisons? - teran
  • @teran and perhaps the maximum and minimum is not so? It's just that I, with the minimum, should just like this or there, specify the $ min variable - Doom'sday
  • Well, you already have an array of minima, why do we need something else to look for and compare. just add it up. or what? - teran
  • @teran yes everything is correct how to sum up that? I am even stupefied at all - Doom'sday
  • If your $min is an array, then obviously $sum =0; foreach($min as $v) $sum += $v $sum =0; foreach($min as $v) $sum += $v ; For some reason, you add up not the minima but arr2[i] , although these are arrays themselves and cannot be added to the number. - teran

2 answers 2

You, of course, have a learning task in order to calculate it all with your hands and cycles, but in general you can solve it as follows. I understand that you need to create an array of 10 random numbers. Then make a two-dimensional array, where the number of elements on the second level will be equal to a random number from the previous array ( this task has already flashed recently ). Then find an array of lows and highs for it. And then count the amount of these minima. ?

Using functions for working with arrays, the solution can be:

 function fill($len){ $result = []; while($len--) $result[] = mt_rand(1, 10); return $result; } $first = fill(10); $second = array_map("fill", $first); $min = array_map('min', $second); $max = array_map('max', $second); $minSum = array_sum($min); print_r( compact('first', 'second', 'min', 'max', 'minSum')); 

For length 3 :

 Array ( [first] => Array ( [0] => 3 [1] => 1 ) [second] => Array ( [0] => Array ( [0] => 9 [1] => 3 [2] => 3 ) [1] => Array ( [0] => 5 ) ) [min] => Array ( [0] => 3 [1] => 5 ) [max] => Array ( [0] => 9 [1] => 5 ) [minSum] => 8 ) 

In principle, if you need to generate all this and count only the sum of the minima, then everything can be entered in one line.

 $smin = array_sum(array_map('min', array_map("fill", fill(10)); 
  • Yes, you're just a genius! - Doom'sday
  • @ Doom'sday laboratory so you will not pass :) - teran
  • I was not looking for this for myself :) but thanks, of course, I haven’t reached the functions yet) but this is awesome ) - Doom'sday

You get an error because $arr2[$i] is an array. Here's how you initialize it $arr2[$i]= []; Naturally, the addition of a number and an array is not an operation that has any meaning.

  • there is still at the stage of the search for minima hat some sort of $max < $arr2[$i] , where max is an array - teran