Jump to content

Array sums


ecopetition

Recommended Posts

Hello, I have the following array:

 

    [0] => Array
        (
            [item_name] => Apples
            [sUM(item_price)] => 19.98
        )

    [1] => Array
        (
            [item_name] => Chicken Breasts
            [sUM(item_price)] => 18.94
        )

    [2] => Array
        (
            [item_name] => Chicken Thighs
            [sUM(item_price)] => 4.78
        )

    [3] => Array
        (
            [item_name] => Yoghurts
            [sUM(item_price)] => 4.56
        )

    [4] => Array
        (
            [item_name] => Shampoo
            [sUM(item_price)] => 3.49
        )

    [5] => Array
        (
            [item_name] => Strawberries
            [sUM(item_price)] => 2.58
        )

    [6] => Array
        (
            [item_name] => Raspberries
            [sUM(item_price)] => 2.58
        )

 

How can I produce a value of the total of all of the "SUM(item_price)" rows?  Also, how can I ask it to return the values for the second rows of the first three entries (to return 19.98, 18.94 and 4.78)?

 

Many thanks in advance.

Link to comment
Share on other sites

Assuming the data is in a multidimensional array called $items, this should work

 

$total = 0;
$counter = 1;
$newarray = array();

//Get total of all items
foreach($items as $v) {

$total = $total + $v["SUM(item_price)"];

//Put first three in $newarray
if($counter < 4) {
	$newarray[] = $v["SUM(item_price)"];
}

$counter++;

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.