Quiero que todos los valores de calificación sumen y luego se dividan con 5
array (size=2) 0 => object(stdClass)[571] public 'rating' => string '3' (length=1) 1 => object(stdClass)[300] public 'rating' => string '5' (length=1)Puede usar un bloque 'foreach', iterar sobre cada elemento en su matriz y resumir su valor de 'calificación'
foreach($array->items as $item) { $sum+=(int)$item->rating; } $result = $sum/5;O puede usar el método array_sum()
$sum =array_sum((int)$array->rating); $result = $sum/5;Solución PHP7:
$result = array_sum(array_column($array_of_objects, 'rating')) / 5;puede usar este código para sumar el valor de la matriz usando php. Primero asigne la variable shich para almacenar todos los valores de suma, por lo que debe establecer 0 y usar el bucle para hacer que todos los valores sumen. este es su código que lo ayuda y si su matriz $your_array entonces
$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 resulty si su matriz es asociativa, use este código
$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 resultrevisa este código y espero que te ayude.