-1
I need to go through the data and find out the maximum number of occurrences of an item. I have the following structure
array(5) {
[0]=>
object(stdClass)#26 (6) {
["num_dia"] => string(1) "5"
["num_semana"] => string(2) "46"
["descricao"] => string(5) "Teste"
}
[1]=>
object(stdClass)#27 (6) {
["num_dia"]=> string(1) "5"
["num_semana"]=>string(2) "46"
["descricao"]=>string(8) "Testeeee"
}
[2]=>
object(stdClass)#28 (6) {
["num_dia"]=>string(1) "6"
["num_semana"]=>string(2) "46"
["descricao"]=>string(5) "Teste"
}
[3]=>
object(stdClass)#29 (6) {
["num_dia"]=>string(1) "6"
["num_semana"]=>string(2) "47"
["descricao"]=>string(7) "Teste 1"
}
[4]=>
object(stdClass)#30 (6) {
["num_dia"]=>string(1) "5"
["num_semana"]=>string(2) "48"
["descricao"]=>string(7) "Bla bla"
}
}
I did it at the time.
$count = 0;
$ativ_max = [];
$atual_max = 0;
$antigo_max = 0;
for ($i=0; $i < count($ativ_semanal); $i++) {
for ($j=0; $j < count($ativ_semanal[$i]->num_semana); $j++) {
if ($i == 0|| $ativ_semanal[$i]->num_semana == $ativ_semanal[$i-1]->num_semana) {
if ($i == 0 || $ativ_semanal[$i]->num_dia == $ativ_semanal[$i-1]->num_dia) {
$atual_max++;
}else{
if ($antigo_max < $atual_max) {
$antigo_max = $atual_max;
if ($i == count($ativ_semanal[$i]->num_semana)) {
$ativ_max = array($ativ_semanal[$i]->num_semana => $antigo_max);
}
}
$atual_max = 1;
}
}else{
var_dump($antigo_max);
$ativ_max = array($ativ_semanal[$i]->num_semana => $antigo_max);
$atual_max=1;
}
}
}
It is creating the associative array
print_r($item); // Array ( [46] => 3 [47] => 1 [48] => 1 )
But only the first one is certain (índice 46
), the second is adding +1
to 2
of índice 46
, he should be 2 and not 3. Since the maximum of items was on the day 5, and were 2 items.
I believe it helps => https://answall.com/q/194196/91
– rray
Good afternoon @rray, actually had already seen this, but it is not exactly what I need, I’m bringing the number of the week
num_semana
from the bank, I need that if on a Monday has 1descricao
and on a Friday it has 5descricao
create an array $arr[num_week] = 5.– Guilherme Rigotti
I need to count as many occurrences of the item that happened in the week.
– Guilherme Rigotti
If each day of the week for example has 1 item, the maximum was 1 and not 5.
– Guilherme Rigotti