php - Sum of a field which stored as an array and group by the same id in laravel 4 -
i have field in table called "images" stored array,
$new_item_photos = item::wherein('id', [10, 15, 10])->get(); foreach ($new_item_photos $new_photos) { if ($new_photos->id == $new_photos->id) { $total_photos += count($new_photos->images); } else { $total_photos = count($new_photos->images); } echo $total_photos."<br>"; }
i result
id photos 10 8 15 9 10 7
now want sum $total_photos , filter duplicate id loop, ex:
id photos 10 15 15 9
can please me on this?
ps: images saved in array format in table
you can use array store count of photos each id. modified example helps further
$new_photos = item::wherein('id', [10, 15, 10])->get(); $total_photos=[]; foreach ($new_photos_dealers $new_photos_dealer) { if ($new_photos->id == $new_photos->id) { if(array_key_exists($new_photos->id,$total_photos)) { $total_photos[$new_photos->id] += count($new_photos->images); } else { $total_photos[$new_photos->id] = count($new_photos->images); { $total_photos[$new_photos->id] += count($new_photos->images); } else { $total_photos[$new_photos->id] = count($new_photos->images); } echo print_r($total_photos, true)."<br>"; }