php - How To Get Array Sum -
i have 2 arrays.
$fruits = [ 'mango' => 20, 'apple' => 30, 'orange' => 10, 'banana' => 5, ]; $purchased = ['mango','banana'];
how sum of values on $fruits
array available on $purchased
array? sum = 25
try loop -
$sum = 0; foreach($purchased $v) { $sum += (!empty($fruits[$v]) ? $fruits[$v] : 0); }
Comments
Post a Comment