Maximum occurrences of an item

Asked

Viewed 49 times

-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

  • 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 1 descricao and on a Friday it has 5 descricao create an array $arr[num_week] = 5.

  • I need to count as many occurrences of the item that happened in the week.

  • If each day of the week for example has 1 item, the maximum was 1 and not 5.

1 answer

1


$itens = [];
foreach ($ativ_semanal as $ativ) {
    if (array_key_exists($ativ->num_semana, $itens)) {
        $itens[$ativ->num_semana]++;
    } else {
        $itens[$ativ->num_semana] = 1;
    }
}

Is that what you’re trying to do? Because it looks like that’s what you want out of it, but it doesn’t match your question. In your question you say you want to count the number of items (which is defined by the description), but in the output you apparently don’t care about the type, just the amount of items in a given week.

  • 1

    I really expressed myself badly, but it’s almost that, only in your example if you happen to have 3 items in the week, 2 on Friday and 1 on Thursday, it will return me 3, but should return 2, I need the maximum of items of the day. I think it’s become clearer what I’m asking

  • Return should be the day that has the most items, not the sum of the items of the week.

  • So why in your example do you say you want $items[46] = 2, none of the days equals 2?

  • Equivalent, they are on the same day. num_dia = 1

  • I updated the array I need to go through, now it gets a little better to see.

  • My return at the moment is being: Array ( [46] => **3** [47] => 1 [48] => 1 ) and now it’s to be Array ( [46] => **2** [47] => 1 [48] => 1 )

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.