开发者

PHP adding simmilar data in a multi-dimensional array

开发者 https://www.devze.com 2023-03-16 15:30 出处:网络
I have a multi dimensional array with some data. ArrayIDDateQuantity 66998[0] [2011-05-11] [50] [1] [2011-05-15] [50]

I have a multi dimensional array with some data.

    Array  ID      Date     Quantity
    66998  [0] [2011-05-11] [50]
           [1] [2011-05-15] [50]
           [2] [2011-05-15] [50]
    81158  [0] [2011-05-11] [25]
           [1] [2011-05-12] [60]
           [2] [2011-05-16] [60]
           [3] [2011-05-18] [60]

I would like to add up all the Quantities from orders within same week. I can calculate the week number with:

            $WeekNumber = date("W", $UnixTimestamp);

So the final array should look like this:

               Week Quantity
    66998  [0] [19] [150]
    81158  [0] [19] [85]
           [1] [20] 开发者_开发技巧[120]    

The array is generated from MS SQL database.


You could use array_reduce($array, 'function_name') to produce the result array one step at a time.

function calc_weeks($result, $row)
{
    . . .
    $WeekNumber = date("W", $UnixTimestamp);
    $result[$weekNumber] += $row['quantity'];
    return $result;
}
array_reduce($array, 'calc_weeks', array());

Alternatively you could use SQL to calculate week numbers and sum your orders that way. In MySQL the function is called WEEK(). For MS SQL there's a related question here.


So operating off of my 3D array theory: (see my comment in OP question)

PHP adding simmilar data in a multi-dimensional array

<?php

    $array = array(66998 => array(array('ID' => 0,
                            'Date' => '2011-05-11',
                            'Quantity' => 50),
                            array('ID' => 1,
                            'Date' => '2011-05-15',
                            'Quantity' => 50),
                            array('ID' => 2,
                            'Date' => '2011-05-15',
                            'Quantity' => 50)
                            ),
                    81158   => array(array('ID' => 0,
                            'Date' => '2011-05-11',
                            'Quantity' => 25),
                            array('ID' => 1,
                            'Date' => '2011-05-12',
                            'Quantity' => 60),
                            array('ID' => 2,
                            'Date' => '2011-05-16',
                            'Quantity' => 60),
                            array('ID' => 3,
                            'Date' => '2011-05-18',
                            'Quantity' => 60)
                            ));

    $newArray = array();

    foreach($array as $subKey => $subArray) {

        foreach($subArray as $idArray) {

            $week = date("W", strtotime($idArray['Date']));

            $newArray["{$subKey}"]["{$week}"] = $idArray['Quantity'] + $newArray["{$subKey}"]["{$week}"];
        }
    }

    var_dump($newArray);

?>

Which yields your desired results (2D for simplicity):

array(2) {
  [66998]=>
  array(1) {
    [19]=>
    int(150)
  }
  [81158]=>
  array(2) {
    [19]=>
    int(85)
    [20]=>
    int(120)
  }
}

Happy coding!

0

精彩评论

暂无评论...
验证码 换一张
取 消