I'm a beginner at php and was searching for a solution all day long without success.
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'.
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.
$out = array();
foreach( $data as $row ) {
$out[$row['my_post_id']] = $row;
}
$array = array_values( $out );
Any help would be much appreciated.
Daniel