如何组合多维数组中的数组并计算值?

时间:2022-01-19 12:12:42

I'm a beginner at php and was searching for a solution all day long without success.

我是php的初学者,整天都在寻找解决方案而没有成功。

I have the following array:

我有以下数组:

$data = Array
(
[0] => Array
    (
        [my_id] => 1
        [my_post_id] => 123
        [my_status] => 1
        [my_rating] => 5
    )

[1] => Array
    (
        [my_id] => 2
        [my_post_id] => 123
        [my_status] => 1
        [my_rating] => 4
    )

[2] => Array
    (
        [my_id] => 3
        [my_post_id] => 123
        [my_status] => 1
        [my_rating] => 5
    )

[3] => Array
    (
        [my_id] => 4
        [my_post_id] => 456
        [my_status] => 1
        [my_rating] => 5
    )

[4] => Array
    (
        [my_id] => 5
        [my_post_id] => 456
        [my_status] => 1
        [my_rating] => 3
    )
)

and would like to merge the arrays with the same 'my_post_id' and count the values for 'my_status' and 'my_rating' which have the same 'my_post_id'.

并希望将数组与相同的'my_post_id'合并,并计算具有相同'my_post_id'的'my_status'和'my_rating'的值。

At the end, I would like to have the following array:

最后,我想有以下数组:

 $data = Array
(
[0] => Array
    (
        [my_post_id] => 123
        [my_status] => 3
        [my_rating] => 14
    )

[1] => Array
    (
        [my_post_id] => 456
        [my_status] => 2
        [my_rating] => 8
    )
)

I could get arrays with unique 'my_post_id' with the following code but I couldn't find out how to count the other values.

我可以使用以下代码获取具有唯一“my_post_id”的数组,但我无法找到如何计算其他值。

$out = array();
    foreach( $data as $row ) {
        $out[$row['my_post_id']] = $row;
    }
    $array = array_values( $out );

Any help would be much appreciated.

任何帮助将非常感激。

Daniel

2 个解决方案

#1


1  

This will produce the array you are looking for:

这将生成您正在寻找的阵列:

$out = array();
foreach( $data as $row ) {
    if (!isset($out[$row['my_post_id']])) {
        $out[$row['my_post_id']] = Array( "my_id"=>$row['my_id'],
                                          "my_status" => $row["my_status"],
                                          "my_rating" => $row["my_rating"]);
    }
    else {
        $out[$row['my_post_id']]["my_status"] += $row["my_status"];
        $out[$row['my_post_id']]["my_rating"] += $row["my_rating"];
    }

}

results in:

Array
(
    [123] => Array
        (
            [my_id] => 1
            [my_status] => 3
            [my_rating] => 14
        )

    [456] => Array
        (
            [my_id] => 4
            [my_status] => 2
            [my_rating] => 8
        )

)

#2


1  

try the following - untested code

尝试以下 - 未经测试的代码

foreach($data as $key => $val){
    if(!array_search($val['my_post_id'],$newArray)){
        $newArray[]=array('my_post_id' => $val['my_post_id'], 
                          'my_status' => $val['my_status'],
                          'my_rating' => $val['my_rating']);
    }else{
        $myIndex=array_search($val['my_post_id'],$newArray);
        $newArray[$myIndex]['my_status']+=$val['my_status'];
        $newArray[$myIndex]['my_rating']+=$val['my_rating'];
    }
}

#1


1  

This will produce the array you are looking for:

这将生成您正在寻找的阵列:

$out = array();
foreach( $data as $row ) {
    if (!isset($out[$row['my_post_id']])) {
        $out[$row['my_post_id']] = Array( "my_id"=>$row['my_id'],
                                          "my_status" => $row["my_status"],
                                          "my_rating" => $row["my_rating"]);
    }
    else {
        $out[$row['my_post_id']]["my_status"] += $row["my_status"];
        $out[$row['my_post_id']]["my_rating"] += $row["my_rating"];
    }

}

results in:

Array
(
    [123] => Array
        (
            [my_id] => 1
            [my_status] => 3
            [my_rating] => 14
        )

    [456] => Array
        (
            [my_id] => 4
            [my_status] => 2
            [my_rating] => 8
        )

)

#2


1  

try the following - untested code

尝试以下 - 未经测试的代码

foreach($data as $key => $val){
    if(!array_search($val['my_post_id'],$newArray)){
        $newArray[]=array('my_post_id' => $val['my_post_id'], 
                          'my_status' => $val['my_status'],
                          'my_rating' => $val['my_rating']);
    }else{
        $myIndex=array_search($val['my_post_id'],$newArray);
        $newArray[$myIndex]['my_status']+=$val['my_status'];
        $newArray[$myIndex]['my_rating']+=$val['my_rating'];
    }
}