How to GROUP BY and SUM PHP Array? [duplicate] - php

This question already has answers here:
Group array data on one column and sum data from another column
(5 answers)
Closed 5 months ago.
How to GROUP BY AND SUM this Array by evaluation_category_id
Array
(
[0] => Array
(
[id] => 60
[evaluation_category_id] => 3
[score] => 15
[itemMaxPoint] => 20
)
[1] => Array
(
[id] => 61
[evaluation_category_id] => 2
[score] => 10
[itemMaxPoint] => 20
)
[2] => Array
(
[id] => 62
[evaluation_category_id] => 1
[score] => 5
[itemMaxPoint] => 20
)
[3] => Array
(
[id] => 63
[evaluation_category_id] => 1
[score] => 50
[itemMaxPoint] => 200
)
[4] => Array
(
[id] => 64
[evaluation_category_id] => 2
[score] => 150
[itemMaxPoint] => 200
)
[5] => Array
(
[id] => 65
[evaluation_category_id] => 3
[score] => 30
[itemMaxPoint] => 50
)
.
.
.
)
So that i get array like this
Array
(
[0] => Array
(
[evaluation_category_id] => 3
[score] => 45
[itemMaxPoint] => 70
)
[1] => Array
(
[evaluation_category_id] => 2
[score] => 160
[itemMaxPoint] => 220
)
[2] => Array
(
[evaluation_category_id] => 1
[score] => 55
[itemMaxPoint] => 220
)
}
i have tried this but its not working .please correct me where i am doing wrong
public function test($data) {
$groups = array();
foreach ($data as $item) {
$key = $item['evaluation_category_id'];
if (!isset($groups[$key])) {
$groups[$key] = array(
'id' => $key,
'score' => $item['score'],
'itemMaxPoint' => $item['itemMaxPoint'],
);
} else {
$groups[$key]['score'] = $groups[$key]['score'] + $item['score'];
$groups[$key]['itemMaxPoint'] = $groups[$key]['itemMaxPoint'] +$item['itemMaxPoint'];
}
}
return $groups;
}
the Output is
Array
(
[2] => Array
(
[id] => 2
[score] => 121 //121 because the given array is different.but its actually SUM all values of score
[itemMaxPoint] => 300
)
)

I have solved it.
public function getDateWiseScore($data) {
$groups = array();
foreach ($data as $item) {
$key = $item['evaluation_category_id'];
if (!array_key_exists($key, $groups)) {
$groups[$key] = array(
'id' => $item['evaluation_category_id'],
'score' => $item['score'],
'itemMaxPoint' => $item['itemMaxPoint'],
);
} else {
$groups[$key]['score'] = $groups[$key]['score'] + $item['score'];
$groups[$key]['itemMaxPoint'] = $groups[$key]['itemMaxPoint'] + $item['itemMaxPoint'];
}
}
return $groups;
}

You could do a Foreach, generate arrays for equal keys, and then array_sum() for each of the arrays values you want to sum :)

Related

Get All Array Values have more than 100 or more, and get array all values 100 with less

My scenario is i have an multidimensional array and i want to get separated array from multidimensional array.
Ex:- First array return which is all values have 100 or more that key and value return as a separated and Second if array has one value less than 100 and other values have a 100 and more that array key and value return as a separated see below example,
Array(
[124] => Array
(
[0] => 140
[1] => 101
[2] => 107
[3] => 116
[4] => 100
)
)
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
)
Array(
[162] => Array
(
[0] => 6
[1] => 79
[2] => 3
)
)
And output should be like this,
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
)
Array(
[162] => Array
(
[0] => 6
[1] => 79
[2] => 3
)
)
Solution with array_filter() and a function filter100(). For explanations of the filter function, see the comments there.
$data = [
124 => [140,101,107,116,100],
164 => [108,111,124,87,278],
162 => [6,79,3],
170 => [6],
171 => [45,34],
172 => [45,134],
];
function filter100($subArr){
$limit = 100;
if(min($subArr) >= $limit) return false; //all values >= 100
$count = count($subArr);
if($count >= 3) return true; //2 values < 100 or 2 values >= 100
//from here on only arrays with 0.1 or 2 values
if($count < 2 OR max($subArr) >= $limit) return false;
return true;
}
$newData = array_filter($data,'filter100');
//test output
echo '<pre>', var_export($newData,true);
Output:
array (
164 =>
array (
0 => 108,
1 => 111,
2 => 124,
3 => 87,
4 => 278,
),
162 =>
array (
0 => 6,
1 => 79,
2 => 3,
),
171 =>
array (
0 => 45,
1 => 34,
),
)
foreach ($total_progress as $key => $value) {
foreach ($value as $key1 => $value1) {
if($value1 > 100 ){
unset($total_progress[$key]);
}
}
}
I have tried with this code but it is removed all the 100 and 100 more values from array and return only 100 less value array. but i want to get array which is 100 and more with 100 less values.
So final output should be like this
Array(
[164] => Array
(
[0] => 108
[1] => 111
[2] => 124
[3] => 87
[4] => 278
)
[170] => Array
(
[0] => 63
[1] => 100
[2] => 45
[3] => 133
[4] => 243
[5] => 170
)
)

How to Sum Array Associative with 2 key values

I'm try to Sum values with 2 keys condition in associative array, but didn't get any result and only not like expected.
my array:
Array
(
[0] => Array
(
[pid] => P1
[rid] => 1
[price] => 100
)
[1] => Array
(
[pid] => P1
[rid] => 1
[price] => 120
)
[2] => Array
(
[pid] => P1
[rid] => 1
[price] => 130
)
[3] => Array
(
[pid] => P2
[rid] => 1
[price] => 80
)
[4] => Array
(
[pid] => P2
[rid] => 1
[price] => 120
)
[5] => Array
(
[pid] => P2
[rid] => 2
[price] => 150
)
);
i have tried some code from
How to GROUP BY and SUM PHP Array? or Grouping arrays in PHP
and then the code becomes:
$groups = array();
foreach ($array as $item) {
$key = $item['pid'];
if (!array_key_exists($key,$groups)) {
$groups[$key] = array(
'pid' => $item['pid'],
'rid'=>$item['rid'],
'price' => $item['price']
);
} else {
$groups[$key]['price'] += $item['price'];
}
}
i exptected output array:
Array
(
[0] => Array
(
[pid] => P1
[rid] => 1
[price] => 350
)
[1] => Array
(
[pid] => P2
[rid] => 1
[price] => 200
)
[2] => Array
(
[pid] => P2
[rid] => 2
[price] => 150
)
);
I have no idea how to write with array_reduce as well as foreach to resolve this, please hit me by other refrence or help me to solve this.
If $data is your input array, you can use below code
$r = array();
foreach ( $data as $d ) {
$key = $d['pid'] . '-' . $d['rid'];
if( !isset ( $r[$key] ) ) {
$r[$key] = $d;
} else {
$r[$key]['price'] += $d['price'];
}
}
echo '<pre>';
print_r($r);
die;

How to manipulate php arrays [duplicate]

This question already has answers here:
Group rows in an associative array of associative arrays by column value and preserve the original first level keys
(7 answers)
Closed 7 years ago.
How to manipulate php arrays. I have a $data variable below.
$data = Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
[2] => Array
(
[id] => 3
[capacity] => 900
[category] => students
)
[3] => Array
(
[id] => 4
[capacity] => 300
[category] => students
)
)
Now I want to group the data with same category like below. . I am not really familiar in php. Can somebody out there help me how to achieve this. What function should I used. Thanks
Array
(
[users] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => users
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => users
)
),
[students] => Array
(
[0] => Array
(
[id] => 1
[capacity] => 900
[category] => students
)
[1] => Array
(
[id] => 2
[capacity] => 300
[category] => students
)
)
)
Just iterate over the array and add it depending on the content of the category field to a new array:
$new = array();
foreach ($data as $val) {
$new[$val['category']][] = $val;
}
This does what you requested
<?php
$data = array(
0 => array (
"id" => 1,
"capacity" => 900,
"category" => "users"
),
1 => array (
"id" => 2,
"capacity" => 300,
"category" => "users"
),
2 => array (
"id" => 3,
"capacity" => 900,
"category" => "students"
),
3 => array (
"id" => 4,
"capacity" => 300,
"category" => "students"
)
);
$groups = array();
foreach ($data as $i) {
if ($i['category'] === "students") {
$groups['students'][] = $i;
}
else if ($i['category'] === "users") {
$groups['users'][] = $i;
}
}
echo "<pre>", print_r($groups), "</pre>";
Here is a working demo - http://codepad.viper-7.com/G4Br28

From multidimensional array i want to extract single column

Input :
Array
(
[1] => Array
(
[id] => 11
[has_point] => 0
[xpr] => 0
[war] => 0
[sty] => 0
)
[2] => Array
(
[id] => 52
[has_point] => 0
[xpr] => 0
[war] => 0
[sty] => 0
)
[3] => Array
(
[id] => 34
[has_point] => 1
[xpr] => 300
[war] => 0
[sty] => 0
)
[4] => Array
(
[id] => 47
[has_point] => 0
[xpr] => 0
[war] => 0
[sty] => 0
)
[5] => Array
(
[id] => 57
[has_point] => 1
[xpr] => 500
[war] => 0
[sty] => 0
)
)
Output:
Array
(
[1] => 11
[2] => 52
[3] => 34
[4] => 47
[5] => 57
)
So is there any function in PHP to obtain this output so i don't have to write my own.
I tried
function customArraySearch($array, $search) {
$col = array();
foreach ($array as $v) {
$col[] = $v[$search];
}
return $col;
}
I am getting my output but I want to optimize my code so if there are 10000 records in array it take some time. Please suggest any inbuilt php function
The function you need is array_column.
$id_array = array_column($your_array, 'id');
Note: Supported version is PHP 5 >= 5.5.0
Try this...
$aa = array(
array(11, 0,0,0,0),
array(52, 0,0,0,0),
array(34, 0,0,0,0),
array(47, 0,0,0,0),
array(57, 0,0,0,0)
);
$newarray=array();
foreach( $aa as $k => list($a, $b,$c,$d,$e)) {
$newarray[]=$a;
}
print_r($newarray);
Supported version is (PHP 5 >= 5.5.0)

How to merge array index if the value is same of another index?

I just pasted my sample input & output.
Sample Input:
Array
(
[0] => Array
(
[id] => 1
[msisdn] => 10
[sc] => 8155
)
[1] => Array
(
[id] => 2
[msisdn] => 20
[sc] => 22020
)
[2] => Array
(
[id] => 3
[msisdn] => 10
[sc] => 8155
)
[3] => Array
(
[id] => 4
[msisdn] => 10
[sc] => 8155
)
[4] => Array
(
[id] => 5
[msisdn] => 20
[sc] => 22020
)
[5] => Array
(
[id] => 6
[msisdn] => 30
[sc] => 22020
)
)
Sample Output:
Array
(
[0] => Array
(
[id] => 1,3,4
[msisdn] => 10
[sc] => 8155
)
[1] => Array
(
[id] => 2,5
[msisdn] => 20
[sc] => 22020
)
[2] => Array
(
[id] => 6
[msisdn] => 30
[sc] => 8155
)
)
Just make that particular value that key, then just concatenate when already pushed/exists:
$new_array = array();
foreach ($array as $value) {
if(!isset($new_array[$value['msisdn']])) {
// if not yet pushed, just initialize
$new_array[$value['msisdn']] = $value;
} else {
// if already inside, then just concatenate
$new_array[$value['msisdn']]['id'] .= ', ' . $value['id'];
}
}
$new_array = array_values($new_array);
echo '<pre>';
print_r($new_array);
Sample Output
Live on codepad: http://codepad.org/0fw9k2w9
You can use the fact that in PHP array is an hash map. By creating an intermediate array you can solve this in O(n) time.
$merged = array();
foreach($array as $v) {
$merged[$v['msisdn']][$v['sc']] [] = $v['id'];
}
$final = array();
foreach($merged as $msisdn=>$v) {
foreach($v as $sc=>$ids) {
$final [] = array('msisdn'=>$msisdn,'sc'=>$sc,'id'=>$ids);
}
}

Categories