I need to go through the data and find out the maximum number of occurrences of an item in a week, for example, if on Monday you have 2 occurrences of the item and on Friday 5 occurrences of the item, you should create an associative array with index = num_week and the value with 5, because it is the maximum of the week.
I have the following structure
array(5) {
[0]=>
object(stdClass)#26 (6) {
["num_day"] => string(1) "5"
["num_week"] => string(2) "46"
["descricao"] => string(5) "Tes"
}
[1]=>
object(stdClass)#27 (6) {
["num_day"]=> string(1) "5"
["num_week"]=>string(2) "46"
["descricao"]=>string(8) "Test"
}
[2]=>
object(stdClass)#28 (6) {
["num_day"]=>string(1) "6"
["num_week"]=>string(2) "46"
["descricao"]=>string(5) "Test"
}
[3]=>
object(stdClass)#29 (6) {
["num_day"]=>string(1) "6"
["num_week"]=>string(2) "47"
["descricao"]=>string(7) "Test"
}
[4]=>
object(stdClass)#30 (6) {
["num_day"]=>string(1) "5"
["num_week"]=>string(2) "48"
["descricao"]=>string(7) "Test"
}
}
I did this right now.
$count = 0;
$max_item = [];
$current_max = 0;
$old_max = 0;
for ($i=0; $i < count($array); $i++) {
for ($j=0; $j < count($array[$i]->num_semana); $j++) {
if ($j == 0 || $array[$j]->num_semana == $array[$j-1]->num_semana) {
if ($j == 0 || $array[$j]->num_dia == $array[$j-1]->num_dia) {
$current_max++;
}else{
if ($old_max < $current_max) {
$old_max = $current_max;
if ($i == count($array[$j]->num_semana)) {
$max_item = array($array[$j]->num_semana => $old_max);
}
}
$current_max = 1;
}
}else{
var_dump($old_max);
$max_item = array($array[$j]->num_semana => $old_max);
$current_max=1;
}
}
}
Return
print_r($ativ_max) // Array ( [48] => 1 )
It is only returning the maximum value of last week (48) and ignoring week 46 and 47.
This code first creates an array with the week as the first dimension and the day as the second, counting the occurrences as it goes through your source data.
Then to get the maximum, it just goes through the weeks and uses array_map()
and max()
to get the highest value for all of the days...
$data = [(object)["num_day" => 1, "num_week" => "2" ],
(object)["num_day" => 1, "num_week" => "2" ],
(object)["num_day" => 2, "num_week" => "2" ],
(object)["num_day" => 4, "num_week" => "2" ],
(object)["num_day" => 4, "num_week" => "2" ],
(object)["num_day" => 4, "num_week" => "2" ],
(object)["num_day" => 4, "num_week" => "3" ],
(object)["num_day" => 4, "num_week" => "3" ],
(object)["num_day" => 4, "num_week" => "4" ]
];
$count = [];
foreach ( $data as $date ) {
if ( !isset ($count[$date->num_week][$date->num_day])) {
$count[$date->num_week][$date->num_day] = 1;
}
else {
$count[$date->num_week][$date->num_day]++;
}
}
$final = array_map("max", $count);
print_r($final);
prints out with the test data...
Array
(
[2] => 3
[3] => 2
[4] => 1
)
Try this. First build an associative array where your week numbers are keys, then get max for each item of that array.
$weeks = Array();
$result = Array();
foreach($structure as $item){
isset($weeks[$item->num_week]) ?
$weeks[$item->num_week][] = $item->num_day :
$weeks[$item->num_week] = Array($item->num_day);
}
foreach($weeks as $week_no => $week){
$result[$week_no] = max($week);
}
var_dump($result)