1

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.

5
  • Where do you get this data from? Commented Jun 29, 2011 at 6:41
  • Would this function help you do it in SQL? (assuming MySQL) Commented Jun 29, 2011 at 6:48
  • But DATEPART(ISO_WEEK, foo) might do the trick, see msdn.microsoft.com/de-de/library/ms174420.aspx Commented Jun 29, 2011 at 6:54
  • 2
    I see a three dimensional array pastebin.com/y3LYKrQQ Commented Jun 29, 2011 at 7:01
  • You are correct, I should rephrase that. :) Commented Jun 29, 2011 at 7:12

2 Answers 2

1

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.

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

1 Comment

Looks plausible, I will try that. Thanks.
1

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

We have to go deeper...

<?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!

1 Comment

I do not know if this is better than the previous answer, but man that picture is funny. Thanks for cheering me up ;D

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.