PHP每小时多维数组的Sum值

时间:2016-06-08 12:22:57

标签: php arrays multidimensional-array

我在$ result

中有这个数组
Array
(
[0] => Array
    (
        [0] => Array
            (
                [buysell] => 270
                [extracthour] => 13
                [date] => 2016-06-06 13:15:00
            )

        [1] => Array
            (
                [buysell] => 7080
                [extracthour] => 14
                [date] => 2016-06-06 14:00:00
            )

        [2] => Array
            (
                [buysell] => 1680
                [extracthour] => 15
                [date] => 2016-06-06 15:00:00
            )

        [3] => Array
            (
                [buysell] => 840
                [extracthour] => 16
                [date] => 2016-06-06 16:00:00
            )

        [4] => Array
            (
                [buysell] => 1320
                [extracthour] => 17
                [date] => 2016-06-06 17:00:00
            )

    )

[1] => Array
    (
        [0] => Array
            (
                [buysell] => 240
                [extracthour] => 0
                [date] => 2016-06-07 00:00:00
            )

        [1] => Array
            (
                [buysell] => 240
                [extracthour] => 2
                [date] => 2016-06-07 02:00:00
            )

        [2] => Array
            (
                [buysell] => 480
                [extracthour] => 4
                [date] => 2016-06-07 04:15:00
            )

        [3] => Array
            (
                [buysell] => 240
                [extracthour] => 5
                [date] => 2016-06-07 05:00:00
            )

        [4] => Array
            (
                [buysell] => 360
                [extracthour] => 7
                [date] => 2016-06-07 07:00:00
            )

等等! 我想在变量中输入这些值作为像这样的数组

 Array
 (
 [0] => array
        (
             [buysell] => (sum of all values in the same extracthour)
             [extracthour] => 1

依此类推,直到我拥有那些日子里每一小时的买入价值。 有人可以解释如何解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助:

function sumBuySells(array $data) {
    $sums = array();
    foreach($data as $day) {
        foreach($day as $hourRow) {
            if (!array_key_exists($hourRow['extracthour'], $sums)) {
                $sums[$hourRow['extracthour']] = 0;
            }
            $sums[$hourRow['extracthour']] += $hourRow['buysell'];
        }
    }
    $result = array();
    foreach($sums as $extracthour => $buysell) {
        $result[] = ['buysell' => $buysell, 'extracthour' = $extracthour];
    }
    return $result;
}

答案 1 :(得分:0)

假设您的整个数组都在变量CREATE TABLE #tempUser( UserId INT, ShopRef INT ) INSERT INTO [User] (UserPassword,Name,MobileNo,Gender,Dob,Country,State,City,StreetAddress ,ZipCode,IsActive ,CreatedDate,ModifiedBy,CreatedBy,IsAdmin,EmailOtp,UserImage,Rating ,ContactNo) OUTPUT inserted.UserId, inserted.EmailOtp INTO #tempUser SELECT 'NA', [Name], [MobileNo], '-','1900-01-01',[Country],[State],[City],[StreetAddress], [ZipCode], 1 ,@Date,@UserId,@UserId,0,ID,'NA',0 ,'NA' FROM #temp WHERE Status ='SUCCESS' 中。请在下面找到代码。希望这可以帮助。

输入:

SET @UserId = SCOPE_IDENTITY()

代码:

$new

输出:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [buysell] => 270
                    [extracthour] => 13
                    [date] => 2016-06-06 13:15:00
                )

            [1] => Array
                (
                    [buysell] => 7080
                    [extracthour] => 14
                    [date] => 2016-06-06 14:00:00
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [buysell] => 240
                    [extracthour] => 0
                    [date] => 2016-06-07 00:00:00
                )

            [1] => Array
                (
                    [buysell] => 240
                    [extracthour] => 2
                    [date] => 2016-06-07 02:00:00
                )

        )

)

答案 2 :(得分:0)

假设您需要从输入获得输出,如下所示: -

输入: -

$arr = array
    (  
    "0" => array  
        (
            "0" => array
                (
                    "buysell" => "270",
                    "extracthour" => "13",
                    "date" => "2016-06-06 13:15:00"
                ),
            "1" => array
                (
                    "buysell" => "7080",
                    "extracthour" => "13",
                    "date" => "2016-06-06 14:00:00"
                ),
            "2" => array
                (
                    "buysell" => "1680",
                    "extracthour" => "2",
                    "date" => "2016-06-06 15:00:00"
                )
        ),
    "1" => array
        (
            "0" => array
                (
                    "buysell" => 240,
                    "extracthour" => 0,
                    "date" => "2016-06-07 00:00:00"
                ),

            "1" => array
                (
                    "buysell" => 240,
                    "extracthour" => 2,
                    "date" => "2016-06-07 02:00:00"
                ),

            "2" => array
                (
                    "buysell" => 480,
                    "extracthour" => 2,
                    "date" => "2016-06-07 04:15:00"
                )
        ),
    );

输出: -

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [buysell] => 7350
                    [extracthour] => 13
                )

            [1] => Array
                (
                    [buysell] => 1680
                    [extracthour] => 2
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [buysell] => 240
                    [extracthour] => 0
                )

            [1] => Array
                (
                    [buysell] => 720
                    [extracthour] => 2
                )
        )
)

代码:

$outputArr = array();
foreach($arr AS $key => $dataArr) {
    foreach ($dataArr AS $eachArr) {
        $outputArr[$key][$eachArr['extracthour']]['buysell'] += $eachArr['buysell'];
        $outputArr[$key][$eachArr['extracthour']]['extracthour'] = $eachArr['extracthour'];
    }
    $outputArr[$key] = array_values($outputArr[$key]);

}

如果您需要单个数组,请删除多维

$outputArr[$eachArr['extracthour']]['buysell'] += $eachArr['buysell'];
$outputArr[$eachArr['extracthour']]['extracthour'] = $eachArr['extracthour'];