How to Sum array value by key for chart? - php

Sorry, I see multiple similar solutions but i'm unable to implement them in my situations. I tried isset to check if value exist before sum but its giving me true value and not able to add its actual value. with bellow code it's repeating the same value as long as for loop running and giving me wrong sum value. Please correct me.
I want to add array value to single array by matching key..
I have this array...
Array(
[0] => Array
(
[Customer] => Array
(
[created_by] => 3
[amount] => 475
)
)
[1] => Array
(
[Customer] => Array
(
[created_by] => 5
[amount] => 199
)
)
[2] => Array
(
[Customer] => Array
(
[created_by] => 1
[amount] => 199
[refund_amount] => 200
[upgrade_amount] => 199
)
)
[3] => Array
(
[Customer] => Array
(
[created_by] => 1
[amount] => 199
)
)
[4] => Array
(
[Customer] => Array
(
[created_by] => 4
[upgrade_amount] => 199
)
)
)
I'm looking for result like this.....
Array(
[3] => Array
(
[sale] => 475
[refund] => 0
[upgrade] => 0
)
[5] => Array
(
[sale] => 199
[refund] => 0
[upgrade] => 0
)
[1] => Array
(
[sale] => 398
[refund] => 200
[upgrade] => 199
)
[4] => Array
(
[sale] => 0
[refund] => 0
[upgrade] => 199
)
)
I'm using this code to get my result....
foreach($records as $y_key => $y_value){
//print_r($y_value);
if(!isset($top_performer[$y_value['Customer']['created_by']])){
$top_performer[$y_value['Customer']['created_by']]['sale'] = 0;
$top_performer[$y_value['Customer']['created_by']]['refund'] = 0;
$top_performer[$y_value['Customer']['created_by']]['upgrade'] = 0;
}
$top_performer[$y_value['Customer']['created_by']]['sale']+= $y_value['Customer']['amount'];
$top_performer[$y_value['Customer']['created_by']]['refund']+= $y_value['Customer']['refund_amount'];
$top_performer[$y_value['Customer']['created_by']]['upgrade']+=$y_value['Customer']['upgrade_amount'];
}

Your code is looking good, you only need to check if array element exists
foreach($records as $y_key => $y_value){
if(!isset($top_performer[$y_value['Customer']['created_by']])){
$top_performer[$y_value['Customer']['created_by']]['sale'] = 0;
$top_performer[$y_value['Customer']['created_by']]['refund'] = 0;
$top_performer[$y_value['Customer']['created_by']]['upgrade'] = 0;
}
$top_performer[$y_value['Customer']['created_by']]['sale'] += isset($y_value['Customer']['amount']) ? $y_value['Customer']['amount'] : 0;
$top_performer[$y_value['Customer']['created_by']]['refund'] += isset($y_value['Customer']['refund_amount']) ? $y_value['Customer']['refund_amount'] : 0;
$top_performer[$y_value['Customer']['created_by']]['upgrade'] += isset($y_value['Customer']['upgrade_amount']) ? $y_value['Customer']['upgrade_amount'] : 0;
}

Related

Sync array values across all related array keys

I have a PHP array with the following data
Array
(
[3] => Array
(
[0] => 4095
[2] => 651
)
[4095] => Array
(
[0] => 3
)
[651] => Array
(
[0] => 4432
)
[4432] => Array
(
[0] => 651
)
[92] => Array
(
[0] => 45
)
)
The above array has keys as student_id and the values are also student_id creating a circular relation. What I am trying to achieve is all the student_id has the same set of student_id values. Basically if student_id 3 is related to 4095, 4432 & 651, then, in turn, each of these values has to have 3 among them including other student_id from 3. The below output demonstrates what I am trying to achieve.
Array
(
[3] => Array
(
[0] => 4095
[1] => 4432
[2] => 651
)
[4095] => Array
(
[0] => 3
[1] => 4432
[2] => 651
)
[651] => Array
(
[0] => 3
[1] => 4432
[2] => 4095
)
[4432] => Array
(
[0] => 3
[1] => 4095
[2] => 651
)
[92] => Array
(
[0] => 45
)
[45] => Array
(
[0] => 92
)
)
Explanation of output
The array keys 3, 4095, 651 & 4432 are related to each other either directly or via a common relation (indirect), so will have common set of values (siblings). The key 92 in input array has a value (sibling) of 45, so in a resultant array, a new key 45 will be added to array with the inverse relation as well.
What I have tried so far
I have tried to do it with this code
$syncedSiblings = [];
foreach ($studentsWithSiblings as $sid => $siblings) {
$all = map_assoc(array_merge([$sid], array_keys($siblings)));
foreach ($all as $studentId) {
if (isset($syncedSiblings[$studentId])) {
$old = $syncedSiblings[$studentId];
$syncedSiblings[$studentId] = array_unique(array_merge($old, array_except($all, $studentId)));
} else {
$syncedSiblings[$studentId] = array_unique(array_except($all, $studentId));
}
}
}
Where $studentsWithSiblings has the above array & array_except returns array without the passed values as second argument.
This is the output I am getting right now
Array
(
[3] => Array
(
[0] => 4095
[1] => 651
)
[4095] => Array
(
[0] => 3
[1] => 651
)
[651] => Array
(
[0] => 3
[1] => 4095
[2] => 4432
)
[4432] => Array
(
[0] => 651
)
[92] => Array
(
[0] => 45
)
)
Any help with this will be highly appreciated.
If I've understood you correctly, then this possible to achieve with recursion:
function getChildren($ind_ar, $prev_ar, $data, $rem){
$tmp = [];
$mark = 0;
foreach($ind_ar as $ind){
foreach($data[$ind] as $new_val){
if(!in_array($new_val,$prev_ar) && $new_val != $ind && $new_val != $rem){
$mark = 1;
$tmp[] = $new_val;
}
foreach($data[$new_val] as $new){
if(!in_array($new,$prev_ar) && $new != $ind && $new != $rem){
$mark = 1;
$tmp[] = $new;
}
}
}
}
$res_ar = $prev_ar;
if(!empty($tmp)) $res_ar = array_unique(array_merge($tmp,$prev_ar));
if($mark) $res_ar = getChildren($tmp,$res_ar,$data, $rem);
return $res_ar;
}
You can use this function in this way:
$data = array( 3 => [4095, 651], 4095 => [3], 651 => [4432], 4432 => [3, 651], 92 => [45], 45 => [92], );
foreach($data as $in => &$data_val) {
$data_val = getChildren([$in],$data_val,$data, $in);
sort($data_val);
}
Demo
Output:
Array
(
[3] => Array
(
[0] => 651
[1] => 4095
[2] => 4432
)
[4095] => Array
(
[0] => 3
[1] => 651
[2] => 4432
)
[651] => Array
(
[0] => 3
[1] => 4095
[2] => 4432
)
[4432] => Array
(
[0] => 3
[1] => 651
[2] => 4095
)
[92] => Array
(
[0] => 45
)
[45] => Array
(
[0] => 92
)
)
Two nested loops over the data. If the keys are different, then check if the key of the inner loop is already contained in the data array of the outer key element - if not, add it.
$data = json_decode('{"3":{"0":4095,"2":651},"4095":[3],"651":[4432],"4432":{"1":651}}', true);
foreach($data as $key_outer => $val_outer) {
foreach($data as $key_inner => $val_inner) {
if($key_outer != $key_inner && !in_array($key_inner, $data[$key_outer])) {
$data[$key_outer][] = $key_inner;
}
}
}
var_dump($data);
This gets you
array (size=4)
3 =>
array (size=3)
0 => int 4095
2 => int 651
3 => int 4432
4095 =>
array (size=3)
0 => int 3
1 => int 651
2 => int 4432
651 =>
array (size=3)
0 => int 4432
1 => int 3
2 => int 4095
4432 =>
array (size=3)
1 => int 651
2 => int 3
3 => int 4095
I am assuming a specific order of the elements in those sub-items is not actually required. If it is, then please sort them yourself as desired afterwards or in between (depending on what exactly you need, f.e. sort($data[$key_outer]); after the inner loop would get you the IDs in all sub-arrays sorted ascending.)

How to make the unique pair of values in array with php

I want to display the unique pairs values in array.and to print the pair only if addition of that pair is even number.Language to be used is PHP.
As you aren't explain well in your question but I tried , Maybe helpful.
<?php
$range1 = range(1,2000);//change as per your requirement
$i = 5; //change as per your requirement
$UniqueEvenPairs = array();
while($i > 0){
shuffle($range1);
$addition = (($range1[0] + $range1[10]));
if($addition % 2 == 0){
$UniqueEvenPairs[$i] = array("val_1"=>$range1[0] , "val_2"=>$range1[10] , "addition" =>$addition);
$i--;
}
}
echo "<pre>";print_r($UniqueEvenPairs);
?>
OutPut
Array
(
[5] => Array
(
[val_1] => 836
[val_2] => 500
[addition] => 1336
)
[4] => Array
(
[val_1] => 293
[val_2] => 319
[addition] => 612
)
[3] => Array
(
[val_1] => 1604
[val_2] => 742
[addition] => 2346
)
[2] => Array
(
[val_1] => 432
[val_2] => 1606
[addition] => 2038
)
[1] => Array
(
[val_1] => 896
[val_2] => 1766
[addition] => 2662
)
)

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
)

Increment counter in a foreach loop depending on condition result

I have an array being generated from a mysql query which looks like the following. They key is the order_id value.
orders array
Array
(
[2646] => Array
(
[order_id] => 2646
)
[2647] => Array
(
[order_id] => 2647
)
[2650] => Array
(
[order_id] => 2650
)
[2658] => Array
(
[order_id] => 2658
)
)
I am looking at adding items to each row of the array with items from another array which looks like.
order_items array
Array
(
[0] => Array
(
[order_id] => 2646
[prod_code] => 2811
[order_qty] => 1
)
[1] => Array
(
[order_id] => 2646
[prod_code] => 2812A
[order_qty] => 3
)
[2] => Array
(
[order_id] => 2647
[prod_code] => 2812A
[order_qty] => 2
)
[3] => Array
(
[order_id] => 2647
[prod_code] => 2810
[order_qty] => 2
)
[4] => Array
(
[order_id] => 2650
[prod_code] => 2906
[order_qty] => 1
)
[5] => Array
(
[order_id] => 2650
[prod_code] => 2908
[order_qty] => 6
)
[6] => Array
(
[order_id] => 2650
[prod_code] => 2909
[order_qty] => 3
)
[7] => Array
(
[order_id] => 2650
[prod_code] => 2913
[order_qty] => 1
)
[8] => Array
(
[order_id] => 2658
[prod_code] => 2880
[order_qty] => 3
)
[9] => Array
(
[order_id] => 2658
[prod_code] => 2881
[order_qty] => 3
)
[10] => Array
(
[order_id] => 2658
[prod_code] => 2882
[order_qty] => 1
)
)
What i am having trouble working out it how to loop over the order_items array and insert the prod_code and order_qty values into the order array where the order_id values match. Here is what i have so far.
// create orders array - key by order_id
while ($order = mysqli_fetch_assoc($order_rows)) {
$orders[$order['order_id']] = $order;
}
// create the order_items array
while ($item = mysqli_fetch_assoc($item_rows)) {
$order_items[] = $item;
}
// update orders array with ordered items
$item_index = 1;
foreach ($order_items as $key => $value) {
// get the order id
$item_order_id = $value['order_id'];
// update orders array with ordered items grouped by order_id
if ($item_order_id == $orders[$item_order_id]['order_id']) {
$orders[$item_order_id]['prod_code_'.$item_index] = $value['prod_code'];
$orders[$item_order_id]['order_qty_'.$item_index] = $value['order_qty'];
// same order id so increment the counter
$item_index++;
} else {
// new order id start at 1
$item_index = 1;
}
}
My problem is that the counter does not reset back to 1 when we are looking at a new item_order_id.
The output i am trying to achieve for my array is something like this.
desired orders array output
Array
(
[2646] => Array
(
[order_id] => 2646
[prod_code_1] => 2811
[order_qty_1] => 1
[prod_code_2] => 2812A
[order_qty_2] => 3
)
[2647] => Array
(
[order_id] => 2647
[prod_code_1] => 2812A
[order_qty_1] => 2
[prod_code_2] => 2810
[order_qty_2] => 2
)
[2650] => Array
(
[order_id] => 2650
[prod_code_1] => 2906
[order_qty_1] => 1
[prod_code_2] => 2908
[order_qty_2] => 6
[prod_code_3] => 2909
[order_qty_3] => 3
[prod_code_4] => 2913
[order_qty_4] => 1
)
[2658] => Array
(
[order_id] => 2658
[prod_code_1] => 2880
[order_qty_1] => 3
[prod_code_2] => 2881
[order_qty_2] => 3
[prod_code_3] => 2882
[order_qty_3] => 1
)
)
Any feedback or assistance greatly appreciated.
You can do two passes:
Aggregate order items by order id
Construct final output
First pass
// create the order_items array
while ($item = mysqli_fetch_assoc($item_rows)) {
$order_items[$item['order_id']][] = $item;
}
Second pass
foreach($order_items as $order_id => $order_items) {
$record = ['order_id' => $order_id];
// add order items
foreach($order_items as $index => $order_item) {
foreach ($order_item as $attribute_name => $attribute_value) {
$key = sprintf('%s_%d', $attribute_name, $index + 1);
$record[$key] = $attribute_value;
}
}
// generate csv row with record here
}

how to merge key array on array

and i got a problem (its big for me) :(
ok, i have some array like ...
Array(
[0] => Array
(
[id] => 1
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[id] => 1
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
[3] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[4] => Array
(
[id] => 2
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
how to merge them if the array have same key value ... ?
the result that i need got is like this ...
Array(
[0] => Array
(
[id] => 1
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/PO/5
[total] => 65
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/4
[total] => 230
)
[2] => Array
(
[order_sn] => EU/2011/04/RS/3
[total] => 130
)
)
)
[2] => Array
(
[id] => 2
[detail] => Array
(
[0] => Array
(
[order_sn] => EU/2011/04/RS/2
[total] => 100
)
[1] => Array
(
[order_sn] => EU/2011/04/RS/1
[total] => 60
)
)
)
)
im very need some help here, and im working on PHP ... what method should i do for this case?
i try too searching on google and in here ... but i dont know the keyword >.<
Many thanks before :)
regard, Stecy
Try like this:
<?php
$result = array();
foreach ($my_array as $v) {
$id = $v['id'];
$result[$id]['id'] = $id;
$result[$id]['detail'][] = array(
'order_sn' => $v['order_sn'],
'total' => $v['total'],
);
}
You can just loop over the array and build a resulting one:
// $a is your array
$r=array();
foreach($a as $v)
$r[$v['id']][]=array('order_sn'=>$v['order_sn'], 'total'=>$v['total']);
echo'<pre>';
var_dump($r);
Since you do the paring by ID, it is wise to have it as the key and all the data associated with it as the value. There's no need to also have id and detail.
foreach($origianlArray as $key => $value){
$newArray[$value['id']]['id'] = $value['id'];
$newArray[$value['id']]['detail'][] = array('order_sn' => $value['order_sn'], 'total' => $value['total']);
}
Check out the PHP array_merge function.

Categories