Jump to content

How to sum in multidimesional array


majma

Recommended Posts

I have such array:

 

 

(
    [0] => Array
        (
            [ownerid] => 100073
            [shipping] => 10
        )

    [1] => Array
        (
            [ownerid] => 107266
            [shipping] => 20
        )

    [2] => Array
        (
            [ownerid] => 107266
            [shipping] => 35
        )

    [3] => Array
        (
            [ownerid] => 100073
            [shipping] => 40
        )
   [4] => Array
        (
            [ownerid] => 123456
            [shipping] => 80
        )
)



 

I'd like to sum  'shipping' for each 'ownerid',  in short the result should look like that:

 

 

(
    [0] => Array
        (
            [ownerid] => 100073
            [shipping] => 50
        )

    [1] => Array
        (
            [ownerid] => 107266
            [shipping] => 55
        )
   [2] => Array
        (
            [ownerid] => 123456
            [shipping] => 80
        )
)



 

Can anyone give me a hint on that?

 

Link to comment
https://forums.phpfreaks.com/topic/132403-how-to-sum-in-multidimesional-array/
Share on other sites

<?php

$testData = array ( array ( 'ownerid'  => 100073,
                            'shipping' => 10
                          ),
                    array ( 'ownerid'  => 107266,
                            'shipping' => 20
                          ),
                    array ( 'ownerid'  => 107266,
                            'shipping' => 35
                          ),
                    array ( 'ownerid'  => 100073,
                            'shipping' => 40
                          ),
                    array ( 'ownerid'  => 123456,
                            'shipping' => 80
                          )
                  );

$testResult = array();
foreach ($testData as $testDatum) {
    $ownerID    = $testDatum['ownerid'];
    $shipping   = $testDatum['shipping'];
    $found = False;
    foreach ($testResult as &$resultDatum) {
        if ($ownerID == $resultDatum['ownerid']) {
            $found = True;
            $resultDatum['shipping'] += $shipping;
        }
    }
    if (!$found) {
        $testResult[] = array( 'ownerid'  => $ownerID,
                               'shipping' => $shipping
                             );
    }
}

echo '<pre>';
print_r($testResult);
echo '</pre>';

?>

<?php

//Your initial array
$shops = array (
    0 => array
        (
            'ownerid' => 100073,
            'shipping' => 10
        ),

    1 => array
        (
            'ownerid' => 107266,
            'shipping' => 20
        ),

    2 => array
        (
            'ownerid' => 107266,
            'shipping' => 35
        ),

    3 => array
        (
            'ownerid' => 100073,
            'shipping' => 40
        ),
    4 => array
        (
            'ownerid' => 123456,
            'shipping' => 80
        )
);


//Create an array that will keep the ownerid as key and the sum of shippings as value
$inter_shops = array ();
foreach ($shops as $data) {
$inter_shops [$data['ownerid']] += $data['shipping'];
}

//You can check if this way is enough
print_r ($inter_shops);

//Create the array anyway you want (the way you want it in this case)
$final_shops = array ();
$count = 0;
foreach ($inter_shops as $ownerid => $shipping) {
$final_shops[$count]['ownerid'] = $ownerid;
$final_shops[$count]['shipping'] = $shipping;

$count++;
}

//See the result
print_r ($final_shops);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.