2
The job is to create 2 arrays with random numbers between 0 and 30. These 2 arrays should be drawn from the most common element to the least common.
Next I have to show to the screen the most common, least common element and which numbers from 0 to 30 are missing in the two arrays.
Right now I can show the screen the most common element, my doubt is on how to discover the least common.
I have already done research on the various functions of Sort (arsort, asort, krsort, etc) but still could not proceed.
The code I have is this::
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>
Totally not a legit website
</title>
</head>
<body>
<?php
function veto($min, $max) {
for ($i = 0; $i < 10; $i++){
$array1[$i] = rand($min, $max);
$array2[$i] = rand($min, $max);
}
// print_r($array1);
// print_r($array2);
$count_1 = array_count_values($array1);
$count_2 = array_count_values($array2);
arsort($count_1); // sortear
arsort($count_2); // descendente
$first_array1 = key($count_1); //mais repetido do array1
$first_array2 = key($count_2); //mais repetido do array2
//------------------------------------------------------------------------------------------------
$count_first_1 = current($count_1);
$count_second_1 = next($count_1);
if($count_first_1 != $count_second_1) { // para saber se existem repetidos no 1º lugar
echo $first_array1 . ' mais comum no primeiro array';
echo "</br>";
} else {
echo $first_array1 . ' - input repetido no mais comum';
echo "</br>";
}
//-------------------------------------------------------------------------------------------------
$count_first_2 = current($count_2);
$count_second_2 = next($count_2);
if($count_first_2 != $count_second_2) { // para saber se existem repetidos no 1º lugar
echo $first_array2 . ' mais comum no segundo array';
echo "</br>";
} else {
echo $first_array2 . ' - input repetido no mais comum';
echo "</br>";
}
print_r ($array1);
echo "</br>";
print_r ($array2);
echo "</br>";
// print_r(end($count_1));
// echo "</br>";
// echo print_r(end($count_2));
// echo "</br>";
}
$min = 0;
$max = 30;
veto($min, $max);
?>
</body>
</html>
Thanks in advance.
I just don’t understand, the element that least appears should be compared in an array or two? I mean vc will 2 numbers (1 per array) 'less frequent'
– rray
Yes, 2 numbers, 1 for each array and yes, it is compared each one in its array.
– Xiri_