Combine php array duplicate values - php

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;

Related

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 merge two array where one array is dynamic nested values

I'm having two arrays like this
$whole_orders
Array
(
[2] => Array
(
[0] => Array
(
[id] => 3
[food_id] => 1
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[food_id] => 2
)
[1] => Array
(
[id] => 1
[food_id] => 1
)
)
)
And $array
Array
(
[2] => Array
(
[0] => Array
(
[count] => 1
[subtotal] => 103.42
[tax] => 18.42
)
)
[1] => Array
(
[0] => Array
(
[count] => 2
[subtotal] => 303.42
[tax] => 38.42
)
)
)
Here I'm having two arrays such as $whole_orders & $array from which I need to merge the $array values into the $whole_orders..
And the $whole_orders having nested values which are dynamic..
Finally My array should be like this..
Array
(
[2] => Array
(
[0] => Array
(
[id] => 3
[food_id] => 1
)
[1] => Array
(
[count] => 1
[subtotal] => 103.42
[tax] => 18.42
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[food_id] => 2
)
[1] => Array
(
[id] => 1
[food_id] => 1
)
[2] => Array
(
[count] => 2
[subtotal] => 303.42
[tax] => 38.42
)
)
)
It should append nested values of $whole_orders array's..
If you think that my title is not correct please change it..
Thanks in advance..
Use foreach and iterate your $array and assigned to $whole_orders
<?php
// if $array is always single dimension array
foreach($array as $array_key=>$array_val)
{
$whole_orders [$array_key][]=$val[0];
}
// or if $array is multi dimension array
foreach($array as $array_key=>$array_val)
{
foreach($array_val as $key=>$val)
{
$whole_orders [$array_key][]=$val;
}
}
?>
Just do this it will achieve you desire output ,but when if the count is same for both array
foreach($array as $arrayKey => $arrayValue){
foreach($arrayValue as $key => $value){
$whole_orders[$arrayKey][] = $value;
}
}
print_r($whole_orders);
You can try below approach..
$arr3 = array();
foreach($arr1 as $key => $value) :
$arr3[$key] = $value;
if(isset($arr2[$key])) :
foreach($arr2[$key] as $k=>$val) :
$arr3[$key][] = $val;
endforeach;
endif;
endforeach;
print_r($arr3);

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
)

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

How do i merge the arrays in a particular format?

I have following arrays:
1) for total placed
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalplaced] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalplaced] => 1
)
)
)
2) for total working
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totalworking] => 4
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totalworking] => 1
)
)
)
3) for total trained
Array
(
[0] => Array
(
[centers] => Array
(
[name] => delhi
[id] => 1
)
[0] => Array
(
[totaltrained] => 8
)
)
[1] => Array
(
[centers] => Array
(
[name] => mumbai
[id] => 2
)
[0] => Array
(
[totaltrained] => 1
)
)
)
I wanted to merge these arrays so that the resultant array should look like this
[newarray] => Array(
[0] => Array (
[centers] => Array
(
[name] => delhi
[id] => 1
[totalplaced] => 8
[totalworking] => 4
[totaltrained] => 8
)
)
[1]=> Array(
[centers] => Array
(
[name] => mumbai
[id] => 2
[totalplaced] => 1
[totalworking] => 1
[totaltrained] => 1
)
)
)
This is the tabular representation of the above data which i want to display
centername totalplaced totalworking totaltrained
delhi 8 4 8
mumbai 1 1 1
Please help me on this.
Thanks
Pankaj Khurana
The difficulty here is that PHP's functions such as array_merge() and array_merge_recursive() will not merge data into numeric keys, but rather will re-key any duplicate numeric key. So for example given two arrays:
array(
'test' => 'abc',
0 => 'xyz'
);
array(
'test' => 'def',
0 => 'uvw'
);
Merging them together with array_merge() will produce an array like:
array(
'test' => 'def',
0 => 'xyz',
1 => 'uvw'
);
So, you need a custom function to be "additive" on any key, regardless of whether it is a string or numeric key. Try this:
function mixed_key_array_merge() {
$args = func_get_args();
$result = array();
foreach ($args as $arg) {
// discard non-array arguments; maybe this could be better handled
if (!is_array($arg)) {
continue;
}
foreach ($arg as $key => $value) {
if (!isset($result[$key])) {
$result[$key] = $value;
} else if (is_array($result[$key])) {
$result[$key] = call_user_func_array('mixed_key_array_merge',array($result[$key],$value));
}
}
}
return $result;
}

Categories