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

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -