Below is my code:
I get ingredients records from database, each of the record contains id, name and weight and category field.
I want to sum the weights of all the ingredients having same id
$weights=array();
foreach($ingredients as $ingredient)
{
$key=array_search($ingredient['id'],$weights);
if($key==true)
{
//Sum weights
}
else
{
$new_ingredient=array('id'=>$ingredient['id'],'weight'=>$ingredient['weight']);
array_push($weights, $new_ingredient);
}
}
print_r($weights);
Currently, I have following:
Meat:
1KG Chicken
3KG Chicken
What I want is:
4KG Chicken
and so on for other categories of ingredients.
So the above foreach loop will run for each of the ingredient category.
I can't somehow handle how to do that, using multidimensional array. Can anyone help, how would I do that? Please let me know if you need more details. Thanks.
var_dump($records_from_mysql_select);