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 this right now.
$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;
}
}
}
He is creating the associative array
print_r($item); // Array ( [46] => 3 [47] => 1 [48] => 1 )
But only the first one is right ( índice 46
), the second is adding +1
to 2
índice 46
, it should be 2 and not 3 . Since the maximum number of items was on the 5 day, and there were 2 items.