How to Sum Array Associative with 2 key values - php

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;

Related

count same categories from array and Store in new array with its count and category name

I have one array which I am getting from database query response.
Now I want to count same categories and print in option_name array.
I have tried it with different array functions but want get desire output.
I have one idea to take new array and push it with foreach loop but not much idea of how can i achieve using code. Please help me to solve it.
Array
(
[0] => Array
(
[CNC] => Array
(
[id] => 5
[category_id] => 68
)
[GVO] => Array
(
[option_name] => Actors
)
)
[1] => Array
(
[CNC] => Array
(
[id] => 11
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[2] => Array
(
[CNC] => Array
(
[id] => 3
[category_id] => 72
)
[GVO] => Array
(
[option_name] => Cricketers
)
)
[3] => Array
(
[CNC] => Array
(
[id] => 4
[category_id] => 74
)
[GVO] => Array
(
[option_name] => Musician
)
)
[4] => Array
(
[CNC] => Array
(
[id] => 7
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
[5] => Array
(
[CNC] => Array
(
[id] => 6
[category_id] => 76
)
[GVO] => Array
(
[option_name] => Directors
)
)
)
Desire Output:
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)
Thanks in advance!
You simply need to loop through the array using foreach like as
$result = [];
foreach($arr as $k => $v){
if(isset($result[$v['GVO']['option_name']])){
$result[$v['GVO']['option_name']] += 1;
}else{
$result[$v['GVO']['option_name']] = 1;
}
}
print_R($result);
You can count the option_name values by incrementing a counter in an associative array where the key is the option_name:
$counts = [];
foreach($array as $v) {
if(!isset($counts[$v['GVO']['option_name']])) {
$counts[$v['GVO']['option_name']] = 0;
}
$counts[$v['GVO']['option_name']]++;
}
print_r($counts);
Try this:
$new_arr = array();
foreach(array_column($your_arr, 'option_name') as $value){
if(in_array($value, $new_array)){
$new_array[$value] = $new_array[$value]+1;
}else{
$new_array[$value] = 1;
}
}
Output
Array
(
[Actors] => 1
[Cricketers] => 2
[Musician] => 1
[Directors] => 2
)

Update quantity session array

I need a help
This is my session array:
Array
(
[menu] =>
[id] => 3
[products] => Array
(
[0] => Array
(
[id] => 1
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 1
[price] => 20
[pricepredtym] => 40
)
[1] => Array
(
[id] => 2
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 1
[price] => 20
[pricepredtym] => 40
)
)
)
I would need about something like, if ($_GET [id] == $ _SESSION ['products'] [id]) and only change this "[pocet]" where [id] = 2
$_GET [id] = 2;
$pocet=5;
[1] => Array
(
[id] => 2
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 5
[price] => 20
[pricepredtym] => 40
)
You could index your products array by product id. Then updating would be simply:
if(isset($_SESSION['products'][$prod_id])) {
$_SESSION['products'][$prod_id]['pocet'] = $pocet;
}
Otherwise, use a foreach loop:
foreach ($_SESSION['products'] as $i => $prod) {
if ($prod['id'] == $prod_id) {
$_SESSION['products'][$i]['pocet'] = $pocet;
break;
}
}
Compare your GET value with 2 and use it as key of SESSION array.
if ($_GET['id'] == '2'){
$_SESSION['products'][$_GET['id']]['pocet'] = '5';
}

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);
}
}

Combine php array duplicate values

I have a php array that just like this:
Array
(
[0] => Array
(
[product] => 2
[price] => 30
[qnty] => 1
)
[1] => Array
(
[product] => 2
[price] => 30
[qnty] => 1
)
[2] => Array
(
[product] => 1
[price] => 250
[qnty] => 1
)
)
and I want to combine the duplicate values, add "qnty" index value and print that array like this:
Array
(
[0] => Array
(
[product] => 2
[price] => 30
[qnty] =>2
)
[1] => Array
(
[product] => 1
[price] => 250
[qnty] => 1
)
)
How can i combine this array. Please help me
try the code below. I am assuming the your array name is $products
$merged = array();
foreach($products as $product) {
$key = $product['product'];
if(!array_key_exists($key, $merged)) {
$merged[$key] = $product;
}
else {
$merged[$key]['qnty'] += $product['qnty'];
}
}
echo '<pre>';
print_r($merged);
exit;

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

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 :)

Categories