Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
How to sum array value php

I want all rating values sum then divided with 5

  array (size=2)
      0 => 
        object(stdClass)[571]
          public 'rating' => string '3' (length=1)
      1 => 
        object(stdClass)[300]
          public 'rating' => string '5' (length=1)
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can either use a 'foreach' block, iterate over each element in your array and sum up its 'rating' value

    foreach($array->items as $item) {
    $sum+=(int)$item->rating;            
    } 
    $result = $sum/5;

Or you can use the array_sum() method

    $sum =array_sum((int)$array->rating);
    $result = $sum/5;
over 4 years ago · Santiago Trujillo Relatório

0

PHP 7 solution:

$result = array_sum(array_column($array_of_objects, 'rating')) / 5;
over 4 years ago · Santiago Trujillo Relatório

0

you can use this code for sum array value using php . First assign variable shich store all sum value so this must set 0 and use loop for make all value sum. this is your code which help you and if your array $your_array then

$sum = 0; // this is store all sum value so first assign 0
foreach ($your_array as $rating)  {          
{
   $sum += $rating->rating; // sum value with previous value and store it and no need to convert string type to int cause php do it 
}
echo $sum; // this is final value
echo $total_sum = $sum / 5; // this is your desire result 

and if your array is associative then use this code

$sum = 0; // this is store all sum value so first assign 0
foreach ($your_array as $rating)  {          
{
   $sum += $rating['rating']; // sum value with previous value and store it and no need to convert string type to int cause php do it 
}
echo $sum; // this is final value
echo $total_sum = $sum / 5; // this is your desire result 

check this code and hope it will help you .

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda