在数组中添加所有相同类型的对象

时间:2019-05-09 07:28:34

标签: php arrays object

我有一系列要在php中压缩的对象。这是我的数组

$proposedStudentFeeCollection = [{"id":1,"student_id":"1","feeType_id":"2","proposed_fee":"5000"},
{"id":2,"student_id":"1","feeType_id":"1","proposed_fee":"5000"},
{"id":3,"student_id":"1","feeType_id":"1","proposed_fee":"2000"},
{"id":4,"student_id":"1","feeType_id":"2","proposed_fee":"15000"},
{"id":5,"student_id":"1","feeType_id":"2","proposed_fee":"5000"},
{"id":6,"student_id":"1","feeType_id":"11","proposed_fee":"9000"},
{"id":7,"student_id":"1","feeType_id":"1","proposed_fee":"20000"},
{"id":8,"student_id":"1","feeType_id":"16","proposed_fee":"1000"}]

我想得到一个“ new ”简化数组,它使我得到相同的feeType_id之和

[{"id":1,"student_id":"1","feetype_id":"1","proposed_fee":"27000"},
{"id":2,"student_id":"1","feetype_id":"2","proposed_fee":"25000"},
{"id":3,"student_id":"1","feetype_id":"11","proposed_fee":"9000"},
{"id":4,"student_id":"1","feetype_id":"16","proposed_fee":"1000"}]

我正在尝试这样做

$myArray = array();
for($i=0; $i<2; $i++){
    $tempObject = $proposedStudentFeeCollection[$i];
    for($j=count($proposedStudentFeeCollection)-1; $j >= 0; $j--) {
        if($tempObject->feetype_id == $proposedStudentFeeCollection[$j]->feetype_id){
            $tempObject->proposed_fee += $proposedStudentFeeCollection[$j]->proposed_fee;
            unset($proposedStudentFeeCollection[$j]);
        }
    }
    $myArray[] = $tempObject;
}
return $myArray;

但是我没有得到答案。正确的方法是什么?

2 个答案:

答案 0 :(得分:0)

创建一个关联数组,该数组将要分组的列用作键。循环遍历原始数组,如果结果中尚不存在该行,则添加该行;否则,将其添加到总计proposed_fee中。

$totals = [];
foreach ($proposedStudentFeeCollection as $e) {
    $id = $e['feeType_id'];
    if (isset($totals[$id])) {
        $totals[$id]['proposed_fee'] += $e['proposed_fee'];
    } else {
        $totals[$id] = $e;
    }
}

答案 1 :(得分:0)

您只需要一个简单的foreach。您必须为每个feeType_id创建一个元素,然后只需将所有费用相加即可。

$id = 1;
$fees = [];
foreach ($proposedStudentFeeCollection as $data) {
    $feeTypeID = $data->feeType_id;
    if (!isset($fees[$feeTypeID])) {
        $fees[$feeTypeID] = new stdClass;
        $fees[$feeTypeID]->id = $id;
        $fees[$feeTypeID]->proposed_fee = 0;
        $fees[$feeTypeID]->student_id = $data->student_id;
        $fees[$feeTypeID]->feeType_id = $data->feeType_id;
        $id++;
    }

    $fees[$feeTypeID]->proposed_fee += $data->proposed_fee;
}

//$fees = array_values($fees);

如果您不希望将费用类型ID用作数组键,只需在array_values上使用$fees(取消注释最后一行)即可。

输出如下:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [proposed_fee] => 25000
            [student_id] => 1
            [feeType_id] => 2
        )

    [1] => stdClass Object
        (
            [id] => 2
            [proposed_fee] => 27000
            [student_id] => 1
            [feeType_id] => 1
        )

    [2] => stdClass Object
        (
            [id] => 3
            [proposed_fee] => 9000
            [student_id] => 1
            [feeType_id] => 11
        )

    [3] => stdClass Object
        (
            [id] => 4
            [proposed_fee] => 1000
            [student_id] => 1
            [feeType_id] => 16
        )

)