0

Lets say I've got an array:

Array
(
    [0] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 36
            [linearFt] => 3
        )

    [1] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 36
            [linearFt] => 3
        )
    [2] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 48
            [linearFt] => 5
        )

    [3] => Array
        (
            [filmType] =>  Symphony 25%
            [rollSize] => 36
            [linearFt] => 7
        )
    [4] => Array
        (
            [filmType] =>  Symphony 35%
            [rollSize] => 36
            [linearFt] => 10
        )
    [5] => Array
        (
            [filmType] =>  Symphony 35%
            [rollSize] => 36
            [linearFt] => 10
        )

How is it possible to add up the [linearFt] of ONLY unique array elements. By unique I mean [filmtype] and [rollSize] the same.

I have tried multible ways including searching array, array filter, and usort... I am new to PHP and this is NOT homework. Thanks for any help!

So my output would be:

$totalsArray = Array(

    [0] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 36
            [linearFt] => 8
        )

    [1] => Array
        (
            [filmType] =>  Symphony 15%
            [rollSize] => 48
            [linearFt] => 3
        )
    [2] => Array
        (
            [filmType] =>  Symphony 25%
            [rollSize] => 48
            [linearFt] => 7
        )
    [3] => Array
        (
            [filmType] =>  Symphony 35%
            [rollSize] => 48
            [linearFt] => 20
        )
1
  • Also, make sure when you ask questions to mark correct answers when received. Your other questions need that attention. (Or people may stop helping) Commented Apr 25, 2012 at 19:38

2 Answers 2

1

Ah. I would attack this with a foreach loop to get each array, then accumulate the different types and sizes and add them as I go. It would look like this:

$filmTotals = array();
$filmTemp   = array();
foreach ($filmInventory as $film){
    $filmTemp[$film['filmType']][$film['rollSize']] = $filmTemp[$film['filmType']][$film['rollSize']] + $film['linearFt'];
}

// A second foreach to resort into the final format, nested to get the keys out to rename them correctly
foreach ($filmTemp as $filmType=>$film){
    foreach ($film as $filmSize=>$filmLength){
        $filmTotals[] = array('filmType'=>$filmType, 'rollSize'=>$filmSize, 'linearFt'=>$filmLength);
    } //END inner foreach
} // END outer foreach
Sign up to request clarification or add additional context in comments.

3 Comments

Have you tested this? I see some glaring issues.
Derp! Yeah, sloppy copypasta, sorry. I've used a similiar structure before...only with everything correctly labeled. Fix'd.
Thank you Malovich! Works like a charm. I will study and learn from your code.
0

Try this:

foreach ($myArray as $value) {
   $sumsArray[$value['filmType']_$value['rollSize']]['length_tally'][] = $value['linearFt'];
}

Will group all similar filmType and rollSize and then you can use array_sum() with the length_tally array to give the total length.

The output won't be exactly what you're looking for, but should put you on the right path.

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.