php在同一个foreach中添加值

时间:2014-07-09 05:45:31

标签: php mysql foreach

我试图将所有出售的书籍数量加起来,例如书籍A卖出55,书籍B卖出9,书籍A 65,书籍B卖出20.书A = 120,书B = 29。

正如你所看到的,我需要将每本书的数值加起来。我只是不确定如何添加值。

我的数组看起来像这样:

Array
(
[Visual Flight Guide] => Array
    (
        [0] => 1
        [30] => 5
        [39] => 3
        [69] => 6
        [118] => 10
    )

[Flight Radio for Pilots  VFR Operations] => Array
    (
        [1] => 1
        [5] => 1
        [17] => 5
        [46] => 1
        [53] => 5
        [120] => 4
    )

[ATC Pilots Logbook] => Array
    (
        [2] => 1
        [3] => 1
        [20] => 30
        [27] => 26
        [28] => 1
        [40] => 8
        [54] => 52
        [66] => 30
        [78] => 52
        [121] => 30
    )

[Aerobatics  Principles and Practice] => Array
    (
        [4] => 1
    )

[Instrument Flight Guide] => Array
    (
        [6] => 5
        [18] => 1
        [32] => 1
        [41] => 6
        [56] => 5
        [70] => 6
        [108] => 8
        [130] => 5
    )

[Command Instrument Rating] => Array
    (
        [7] => 10
        [16] => 1
        [35] => 1
        [57] => 5
        [67] => 6
        [93] => 3
        [113] => 2
    )

[Student Pilot Kit] => Array
    (
        [8] => 5
        [13] => 4
        [44] => 1
        [51] => 9
        [61] => 12
        [80] => 1
        [102] => 5
        [117] => 2
        [134] => 1
    )

[Basic Aeronautical Knowledge] => Array
    (
        [9] => 10
        [29] => 5
        [52] => 5
        [63] => 6
        [79] => 20
        [94] => 1
        [106] => 1
        [110] => 12
        [119] => 4
    )

[Human Factors] => Array
    (
        [10] => 10
        [22] => 1
        [45] => 1
        [83] => 6
        [122] => 3
    )

[ATC Rotating Plotter] => Array
    (
        [11] => 4
    )

[ATC Protractor] => Array
    (
        [12] => 5
        [42] => 12
        [60] => 10
        [74] => 30
        [81] => 25
    )

[180NM Scale Rule] => Array
    (
        [14] => 4
        [43] => 12
        [73] => 30
        [82] => 25
    )

[Seven Volume PPLCPL Kit] => Array
    (
        [15] => 4
        [19] => 1
        [31] => 1
        [62] => 6
        [77] => 1
        [100] => 3
        [128] => 3
    )

[Fake Item] => Array
    (
        [21] => 1
    )

[Flight Rules and Air Law] => Array
    (
        [23] => 1
        [87] => 7
        [97] => 1
        [127] => 5
    )

[Aircraft Operation Performance and Planning] => Array
    (
        [24] => 1
        [88] => 4
        [95] => 1
        [112] => 6
        [133] => 1
    )

[Aerodynamics] => Array
    (
        [25] => 1
        [91] => 1
        [101] => 1
        [104] => 1
        [124] => 3
    )

[Aircraft General Knowledge] => Array
    (
        [26] => 1
        [84] => 5
        [92] => 1
        [99] => 1
        [105] => 1
        [123] => 3
    )

[Human Being Pilot] => Array
    (
        [33] => 1
        [68] => 3
        [109] => 16
    )

[Flight Rules and Air Law for the Air Transport Pilot] => Array
    (
        [34] => 1
        [55] => 3
        [65] => 3
        [89] => 1
        [129] => 2
    )

[Aerodynamics Engines  Airframe Systems for the Air Transport Pilot] => Array
    (
        [36] => 1
    )

[Aeroplane Performance Planning  Loading for the Air Transport Pilot] => Array
    (
        [37] => 1
        [98] => 1
    )

[Avionics and Flight Management Systems for the Air Transport Pilot] => Array
    (
        [38] => 1
    )

[The Flying Training Manual] => Array
    (
        [47] => 1
        [48] => 1
        [103] => 6
        [107] => 10
        [116] => 2
    )

[In Safe Hands] => Array
    (
        [49] => 3
    )

[Helicopter Trial Instructional Flight] => Array
    (
        [50] => 3
    )

[Flight at Lower Levels  Safety in the Circuit] => Array
    (
        [58] => 3
    )

[Night Flight] => Array
    (
        [59] => 3
        [75] => 4
        [114] => 1
        [132] => 1
    )

[Meteorology] => Array
    (
        [64] => 3
        [85] => 7
        [111] => 6
        [125] => 3
    )

[80NM Scale Rule] => Array
    (
        [71] => 10
    )

[120NM Scale Rule] => Array
    (
        [72] => 10
    )

[MultiEngine Piston] => Array
    (
        [76] => 2
    )

[Navigation] => Array
    (
        [86] => 5
        [96] => 2
        [126] => 3
    )

[Three Points  Flying a Tailwheel Aircraft] => Array
    (
        [90] => 1
    )

[Pilots Index] => Array
    (
        [115] => 1
        [131] => 5
    )

)

我试过这个:

$stock_taken = array();
    $i = 0; 
    foreach($stock_take as $stock){
        $stock_taken[$stock->name][$i] = $stock->qty;
        $i++;
    }

1 个答案:

答案 0 :(得分:0)

你必须使用array_sum()作为array的值之和。所以试试这个

//$array = your array
$sum = array();
foreach($array as $key=>$stock)
{
     $sum[$key]=array_sum($stock);  
}

foreach($sum as $key=>$values)
{
echo "sum of book ".$key." sold is ".$values."\n";
}

Demo