Foreach数组爆炸量总量

时间:2016-09-01 05:55:59

标签: php arrays

这是我的数组我需要添加所有金额,如果它们在一起,例如1000 | 1000

  Array
    (
        [0] => stdClass Object
            (
                [student_no] => 8080808128
                [amt] => 1000|1000
                [main_receipt] => M_0000000001
            )

        [1] => stdClass Object
            (
                [student_no] => 8080808128
                [amt] => 500
                [main_receipt] => M_312312321
            )

        [2] => stdClass Object
            (
                [student_no] => 8569544855
                [amt] => 500
                [main_receipt] => M_000000026
            )

        [3] => stdClass Object
            (
                [student_no] => 9172544146
                [amt] => 1000
                [main_receipt] => M_00000001234
            )

        [4] => stdClass Object
            (
                [student_no] => 9541256358
                [amt] => 1000|1000|500
                [main_receipt] => M_000000022
            )

        [5] => stdClass Object
            (
                [student_no] => 9892469054
                [amt] => 1000
                [main_receipt] => M_0000000002
            )

    )

$newar=0;
foreach($fa as $key){

       $temp= explode('|',$key->amt);
foreach($temp as $tem =>$va){
echo $va."<br>";
  $newar += $va;
    $array[]=$newar;

我试图构建一个新的数组,其中amt总计

例如

[0] => stdClass Object
                (
                    [student_no] => 8080808128
                    [amt] => 2000
                    [main_receipt] => M_0000000001
                )
  [1] => stdClass Object
                (
                    [student_no] => 8080808128
                    [amt] => 500
                    [main_receipt] => M_312312321
                )

            [2] => stdClass Object
                (
                    [student_no] => 8569544855
                    [amt] => 500
                    [main_receipt] => M_000000026
                )

            [3] => stdClass Object
                (
                    [student_no] => 9172544146
                    [amt] => 1000
                    [main_receipt] => M_00000001234
                )

注意[0]键数组中总计金额值的方式。我试图改变这种类型的数组,但我似乎无法得到逻辑.......

我知道我当前的逻辑只是添加数组

的每个键的值

3 个答案:

答案 0 :(得分:3)

在|时拆分你的amt出现并使用 array_sum()

计算总和
foreach($data as &$array) {
   $array->amt = array_sum(explode('|', $array->amt));
}

答案 1 :(得分:0)

在循环显示爆炸量之前,您需要将$newar设置回0。否则,您将所有对象中的金额一起添加。您也可以使用array_sum()来计算爆炸量的所有元素。

如果您想要新数组中的对象,则需要创建对象,而不仅仅是将数据存储在数组中。

foreach ($fa as $obj) {
    $temp = explode('|', $obj->amt);
    $newar = array_sum($temp);
    $newobj = clone $obj;
    $newobj->amt = $newar;
    $array[] = $newobj;
}

答案 2 :(得分:0)

According to your goal:

"to construct a new array where the amt is totaled"

use the following solution with array_map, strpos(to check if there's a delimiter | within amt property to avoid redundant splitting) and array_sum functions:

// $arr is your initial array
$new_arr = array_map(function($obj){
    $obj->amt = (strpos($obj->amt, "|") !== false)? array_sum(explode("|", $obj->amt)) : $obj->amt;
    return $obj;
}, $arr);

print_r($new_arr);