【问题标题】:Reduce arrays inside array of object减少对象数组内的数组
【发布时间】:2018-05-29 15:43:50
【问题描述】:

我有这个数组:

Collection {#319 ▼
  #items: array:6 [▼
    "Seccion 1 Pregunta 1" => array:3 [▼
      "satisfactory" => 2
      "unsatisfactory" => 0
      "total" => 2
    ]
    "Seccion 1 Pregunta 2" => array:3 [▼
      "satisfactory" => 2
      "unsatisfactory" => 0
      "total" => 2
    ]
    "Seccion 1 pregunta 3" => array:3 [▼
      "satisfactory" => 0
      "unsatisfactory" => 1
      "total" => 1
    ]
    "Seccion 2 pregunta 1" => array:3 [▼
      "satisfactory" => 3
      "unsatisfactory" => 0
      "total" => 3
    ]
    "Seccion 2 pregunta 2" => array:3 [▼
      "satisfactory" => 1
      "unsatisfactory" => 1
      "total" => 2
    ]
    "Commentarios seccion 2" => array:3 [▼
      "satisfactory" => 0
      "unsatisfactory" => 0
      "total" => 0
    ]
  ]
}

我想得到所有满意、不满意和总值的总和。比如:

Collection {#319 ▼
    #items: array:3 [▼
          "satisfactory" => 8
          "unsatisfactory" => 2
          "total" => 10
    ]
}

【问题讨论】:

标签: php laravel


【解决方案1】:

如果你知道你想要的钥匙,就这样做:

[
    'satisfactory'   => $collection->sum('satisfactory'),
    'unsatisfactory' => $collection->sum('unsatisfactory'),
    'total'          => $collection->sum('total')
]

如果您不确定键是什么,请对第一项的键进行循环以创建与上述类似的数组。如果您需要将其作为一个集合,只需执行以下操作:

$newCollection = collect( ... array from above ... );

【讨论】:

    【解决方案2】:

    你可以试试这样的:

    $totals = array();
    foreach($array as $key => $val) {
        $totals[$key] += $val;
    }
    

    【讨论】:

      【解决方案3】:

      您也可以使用 array_reduce 并传递一个键和值设置为 0 的数组作为起始值。

      $result = array_reduce($arrays, function($carry, $item){
          $carry["satisfactory"] += $item["satisfactory"];
          $carry["unsatisfactory"] += $item["unsatisfactory"];
          $carry["total"] += $item["total"];
          return $carry;
      }, ["satisfactory" => 0, "unsatisfactory" => 0, "total" => 0]);
      
      print_r($result);
      

      这会给你:

      Array
      (
          [satisfactory] => 8
          [unsatisfactory] => 2
          [total] => 10
      )
      

      【讨论】:

        【解决方案4】:

        你可以使用array_walk:

        $data = [
        "Seccion 1 Pregunta 1" => [
          "satisfactory" => 2,
          "unsatisfactory" => 0,
          "total" => 2
        ],
        "Seccion 1 Pregunta 2" => [
          "satisfactory" => 2,
          "unsatisfactory" => 0,
          "total" => 2
        ],
        "Seccion 1 pregunta 3" => [
          "satisfactory" => 0,
          "unsatisfactory" => 1,
          "total" => 1
        ],
        "Seccion 2 pregunta 1" => [
          "satisfactory" => 3,
          "unsatisfactory" => 0,
          "total" => 3
        ],
        "Seccion 2 pregunta 2" => [
          "satisfactory" => 1,
          "unsatisfactory" => 1,
          "total" => 2
        ],
        "Commentarios seccion 2" => [
          "satisfactory" => 0,
          "unsatisfactory" => 0,
          "total" => 0
        ]
        ];
        
        $total = array('satisfactory' => 0, 'unsatisfactory' => 0, 'total' => 0);
        
        array_walk($data, function($v) use (&$total) {
           $total['satisfactory'] += $v['satisfactory'];
            $total['unsatisfactory'] += $v['unsatisfactory'];
            $total['total'] += $v['total'];
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-26
          • 1970-01-01
          • 1970-01-01
          • 2018-09-03
          • 2020-12-30
          • 2021-04-05
          • 2018-09-01
          • 1970-01-01
          相关资源
          最近更新 更多