Increment counter in a foreach loop depending on condition result - php

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
}

Related

get the sum value of specific key in given array and add row of count after specific completed index

Actual I get
Array
(
[0] => Array
(
[name] => person1
[value] => 11
)
[1] => Array
(
[name] => person2
[value] => 5
)
[2] => Array
(
[name] => person2
[value] => 5
)
[3] => Array
(
[name] => person4
[value] => 10
)
)
Actually i need
Array
(
[0] => Array
(
[name] => person1
[value] => 11
)
//here i want new row index 1 array
[1] => Array
(
[name] => total
[value] => 11 //this value is the sum of index 1
)
[2] => Array
(
[name] => person2
[value] => 5
)
[3] => Array
(
[name] => person2
[value] => 5
)
// here i want add new line index 4 array
[4] => Array
(
[name] => total
[value] => 10 //this value is the sum of index 2 and 3
)
[5] => Array
(
[name] => person4
[value] => 10
)
// here i want add new line index 6 array
[6] => Array
(
[name] => total
[value] => 10 //this value is the sum of index 5
)
)
may this help you i have tried some step making grouping but sum is not correct but code may help you
$a = Array
(
0 => Array
(
'name' => 'person1',
'value' => 11,
),
1 => Array
(
'name' => 'person2',
'value' => 5,
),
2 => Array
(
'name' => 'person2',
'value' => 5,
),
3 => Array
(
'name' => 'person4',
'value' => 10,
)
);
foreach($a as $c){
$d[]=$c['name'];
$e[]=$c['value'];
}
//print_r($d);
$group = array();
foreach($d as $key=>$val){
$group[$val][] = $e[$key];
}
// this loop for check the max number and count total price
$data = array();
$total = 0;
foreach($group as $key=>$val){
for($i=0;$i<count($val);$i++){
$data[]['name'] = $key;
$data[]['value'] = $val[$i];
$suma[] = $val[$i];
}
$sunmb =array_sum($suma);
$data[]['name'] = 'total';
$data[]['value'] = $sunmb;
}
print_r($data);
You can check code run here https://wtools.io/php-sandbox/b1LD

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;

Multi-dimensional array return keys with duplicate values

Please Help! I want to validate array with duplicate sub-array values. I have an multi-dimensional array. I want to return keys of sub-array with duplicate product_id value. Example: In my array, I have sub-array with duplicate product_id = 124. I want to return their key.
[purchase_order_products] => Array
(
[0] => Array
(
[product_id] => 124
[barcode] => 480001407081
[item_code] =>
[name] => Brew Kettle Can 330mL
[qty] =>
[unit] => 2
[pcs_have] => 1
[total_pcs] => 1
[cost] => 34.83
[total_item_price] => 34.83
[stocks] =>
[po_qty] =>
)
[1] => Array
(
[product_id] => 125
[barcode] => 480001416108
[item_code] =>
[name] => Colt 45 Can 330mL
[qty] =>
[unit] => 2
[pcs_have] => 1
[total_pcs] => 1
[cost] => 29.58
[total_item_price] => 29.58
[stocks] =>
[po_qty] =>
)
[2] => Array
(
[product_id] => 124
[barcode] => 480001407081
[item_code] =>
[name] => Brew Kettle Can 330mL
[qty] =>
[unit] => 2
[pcs_have] => 1
[total_pcs] => 1
[cost] => 34.83
[total_item_price] => 34.83
[stocks] =>
[po_qty] =>
)
)
The output I want is:
Array(0,2)
Edit: I've updated the answer quite a bit.
Edit 2: Now utilizing the built in array functions to find the duplicates
$products = [
0 => ['product-id' => 124],
1 => ['product-id' => 125],
2 => ['product-id' => 124],
3 => ['product-id' => 126],
4 => ['product-id' => 126],
8 => ['product-id' => 124],
];
// Find the duplicates
$product_ids = array_column($products, 'product-id');
$count = array_count_values($product_ids);
$duplicates = array_filter($count, function($var) {
return $var > 1;
});
// List all the entries with duplicate ids:
foreach ( array_flip($duplicates) as $product_id ) {
$filter = array_filter($products, function($var) use ($product_id) {
return ( $var['product-id'] === $product_id );
});
print_r('Product-id: ' . $product_id . ' is duplicated in entries: ');
print_r(array_keys($filter));
}
The output:
// Product-id: 124 is duplicated in entries: Array
// (
// [0] => 0
// [1] => 2
// [2] => 8
// )
// Product-id: 126 is duplicated in entries: Array
// (
// [0] => 3
// [1] => 4
// )
Use this code to get key for duplicate product id:
$products = $array['purchase_order_products'];
$duplicate_products_keys = array();
$products_ids = array();
foreach($products as $key => $product) {
if(in_array($product['product_id'], $products_ids)) {
$duplicate_products_keys[] = $key;
}
$products_ids[$product['product_id']] = $product['product_id'];
}
prinr_r($duplicate_products_keys);

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

Categories