0

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.

4
  • No the scenario is different here...I know that is solution in many cases but not applicable here. Why not applicable? I can't explain but I am sure db query solution is not applicable here. I want to sum that using arrays. Thanks for your help. Commented Jun 4, 2014 at 8:06
  • So what you're after is to remove all duplicate items and calculate their sums to the remaining item? Commented Jun 4, 2014 at 8:08
  • Yes @Antti29. That is what I want, but only using similar way I wrote in code, not using SQL sum function. Commented Jun 4, 2014 at 8:09
  • maybe you could post a sample you a simple select db like: var_dump($records_from_mysql_select); Commented Jun 4, 2014 at 8:20

2 Answers 2

1

To sum them all up, you need to create another array from that, them sum the weight key while inside the loop. Consider this example:

// if your db query result looks like this (we dont know what your original data looks like)
// sample data from select * from ingredients
$values_from_db = array(
    array(
        'id' => 1,
        'name' => 'Chicken',
        'unit' => 'KG',
        'weight' => 100,
    ),
    array(
        'id' => 2,
        'name' => 'Pork',
        'unit' => 'KG',
        'weight' => 300,
    ),
    array(
        'id' => 3,
        'name' => 'Beef',
        'unit' => 'KG',
        'weight' => 400,
    ),
    array(
        'id' => 1,
        'name' => 'Chicken',
        'unit' => 'KG',
        'weight' => 100,
    ),
    array(
        'id' => 2,
        'name' => 'Pork',
        'unit' => 'KG',
        'weight' => 200,
    ),
);

$data = array();
foreach($values_from_db as $key => $value) {
    $current_id = $value['id'];
    if(!isset($data[$current_id]['weight'])) $data[$current_id]['weight'] = 0;
    $data[$current_id]['id'] = $current_id;
    $data[$current_id]['name'] = $value['name'];
    $data[$current_id]['unit'] = $value['unit'];
    $data[$current_id]['weight'] += $value['weight'];
}

echo "<pre>";
print_r($data);
echo "</pre>";

Sample Output:

Array
(
    [1] => Array
        (
            [weight] => 200
            [id] => 1
            [name] => Chicken
            [unit] => KG
        )

    [2] => Array
        (
            [weight] => 500
            [id] => 2
            [name] => Pork
            [unit] => KG
        )

    [3] => Array
        (
            [weight] => 400
            [id] => 3
            [name] => Beef
            [unit] => KG
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

If the key of the result array is not important, you could do the following:

$result = array();

foreach ($ingredients as $ingredient) {
    if (isset($result[$ingredient['id']]))
        $result[$ingredient['id']]['weight'] += $ingredient['weight'];
    else
        $result[$ingredient['id']] = $ingredient;
}

The id of the item is stored in the key of $result and used when checking if the item is already there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.