I'm trying to merge and calc only by choosen key. For example I have the following array:
$array = array();
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 2];
$array['Fruit'][] = ['id' => 17, 'name' => 'seasonal fruit', 'type' => 'Fruit', 'measurement' => '', 'amount' => 1];
$array['protein'][] = ['id' => 16, 'name' => 'soy yogurt', 'type' => 'protein', 'measurement' => '', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 2];
$array['protein'][] = ['id' => 18, 'name' => 'oilseed', 'type' => 'protein', 'measurement' => 'grip', 'amount' => 1];
$array['lipid'][] = ['id' => 19, 'name' => 'coconut powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 20, 'name' => 'chocolate powder', 'type' => 'lipid', 'measurement' => 'Tablespoon', 'amount' => 2];
$array['lipid'][] = ['id' => 38, 'name' => 'chocolate square', 'type' => 'lipid', 'measurement' => '', 'amount' => 1];
Final output should look like this :
Array (
[0] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
[1] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[2] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
[3] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[4] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[5] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
I tried the following :
$courses = [];
foreach ($array as $key => $item) {
if (!key_exists($key, $courses)) {
$courses[$key] = [];
}
foreach ($item as $row) {
$id = $row['id'];
if (!key_exists($id, $courses[$key])) {
$courses[$key][$id] = $row;
continue;
}
$r = &$courses[$key][$id];
$r['amount'] += $row['amount'];
}
}
Output:
Array (
[Fruit] => Array (
[17] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
)
[protein] => Array (
[16] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[18] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
)
[lipid] => Array (
[19] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[20] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[38] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
)
You were not far off, but everything you are interested in ins inside the inner foreach, that way you dont get the Fruit and Protien etc involved
$courses = [];
foreach( $array as $sub){
foreach ($sub as $item) {
if ( array_key_exists($item['id'], $courses) ) {
// dup, so just add to the amount
$courses[$item['id']]['amount'] += $item['amount'];
} else {
//New entry
$courses[$item['id']] = $item;
}
}
}
// if its important to have indexes starting at 0 for the result
$courses = array_values($courses);
Related
Am totally confused with the multidimensional arrays in php. I have a very big array in which am trying to do some search. That is if the colour is green, check for the age and resultant array should contain 4 highest value of age with colour green and all other subarrays should be unaffected. Please help
Array
(
[0] => Array
(
[name] => arr1
[data] => Array
(
[0] => Array
(
[name] => A
[age] => 5
[color] => green
)
[1] => Array
(
[name] => B
[age] => 4
[color] => green
)
[2] => Array
(
[name] => C
[age] => 10
[color] => Red
)
[3] => Array
(
[name] => F
[age] => 1
[color] => green
)
)
)
[1] => Array
(
[name] => arr2
[data] => Array
(
[0] => Array
(
[name] => cc
[age] => 8
[color] => yellow
)
[1] => Array
(
[name] => Y
[age] => 20
[color] => green
)
[2] => Array
(
[name] => Y
[age] => 9
[color] => green
)
)
)
)
Expected resultant array is
Array
(
[0] => Array
(
[name] => arr1
[data] => Array
(
[0] => Array
(
[name] => A
[age] => 5
[color] => green
)
[1] => Array
(
[name] => B
[age] => 4
[color] => green
)
[2] => Array
(
[name] => C
[age] => 10
[color] => Red
)
)
)
[1] => Array
(
[name] => arr2
[data] => Array
(
[0] => Array
(
[name] => cc
[age] => 8
[color] => yellow
)
[1] => Array
(
[name] => Y
[age] => 20
[color] => green
)
[2] => Array
(
[name] => Y
[age] => 9
[color] => green
)
)
)
)
<?php
// Your code here!
$ar[0] = array('name' => 'arr1',
'data' => array
(
'0' => array
(
'name' => 'A',
'age' => 5,
'color' => 'green'
),
'1' => array
(
'name' => 'B',
'age' => 4,
'color' => 'green'
),
'2' => array
(
'name' => 'C',
'age' => 10,
'color' => 'Red'
),
'3' => array
(
'name' => 'F',
'age' => 1,
'color' => 'green'
)
)
);
$ar[1] = array
(
'name' => 'arr2',
'data' => array
(
'0' => array
(
'name' => 'cc',
'age' => 8,
'color' => 'yellow'
),
'1' => array
(
'name' => 'Y',
'age' => 20,
'color' => 'green'
),
'2' => array
(
'name' => 'Y',
'age' => 9,
'color' => 'green'
)
)
);
$green = array();
foreach($ar as $k1=>$a1){
foreach($a1['data'] as $k2=>$a2){
if($a2['color']=='green') {
array_push($green,$a2['age']);
}
}
}
rsort($green);
$green = array_splice($green,0,4);
foreach($ar as $k1=>$a1){
foreach($a1['data'] as $k2=>$a2){
if($a2['color']=='green') {
if(!in_array($a2['age'], $green)){
unset($ar[$k1]['data'][$k2]);
}
}
}
}
print_r($ar);
?>
I have two multiple select input result that i want to join. Its about delivery destination and delivery fee.
here is my array result :
Array
(
[destination] => Array
(
[0] => London
[1] => Liverpool
[2] => Nottingham
[3] => Oxford
)
[fee] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
)
)
And I want to push these value to each array :
$status = "1";
Result I expected is :
Array
(
[0] => Array
(
[destination] => London
[fee] => 10
[status] => 1
)
[1] => Array
(
[destination] => Liverpool
[fee] => 15
[status] => 1
)
[2] => Array
(
[destination] => Nottingham
[fee] => 20
[status] => 1
)
[3] => Array
(
[destination] => Oxford
[fee] => 25
[status] => 1
)
)
Thanks for helping me.
$array = ['destination' => ['London', 'Liverpool', 'Nottingham', 'Oxford'], 'fee' => [10, 15, 20, 25]];
$result = [];
foreach ($array['destination'] as $index => $value)
{
$result[] = ['destination' => $value, 'fee' => $array['fee'][$index], 'status' => 1];
}
Try this: https://3v4l.org/004PF
<?php
$givenArray = [
'destination' => [
'London',
'Liverpool',
'Nottingham',
'Oxford',
],
'fee' => [
10,
15,
20,
25
],
];
$output = [];
foreach ($givenArray['destination'] as $key => $destination) {
$fee = $givenArray['fee'][$key];
$output[] = [
'destination' => $destination,
'fee' => $fee,
'status' => 1,
];
}
print_r($output);
Output is:
Array
(
[0] => Array
(
[destination] => London
[fee] => 10
[status] => 1
)
[1] => Array
(
[destination] => Liverpool
[fee] => 15
[status] => 1
)
[2] => Array
(
[destination] => Nottingham
[fee] => 20
[status] => 1
)
[3] => Array
(
[destination] => Oxford
[fee] => 25
[status] => 1
)
)
I have 2 arrays category and product_to_category (Structure shown below)
[category] => Array
(
[0] => 299
[2] => 342
[3] => 134
[4] => 333
[5] => 347
[9] => 296
)
And
[product_to_category] => Array
(
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.73432159423828
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.73432159423828
)
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.73432159423828
)
....So On (5000+ records)
i have almost 5000+ records in product_to_category array, But i want to pick only 3 products from P2C array per Category. For Example category_id=299 so 3 products of 299 will be added to my new array and same for other categories.
Is there any possible way to do that .
Try this query to fetch record you wanted,
select p2c.product_id, p2c.category_id, p2c.relevance
from category c
LEFT JOIN product_to_category p2c ON p2c.category_id = category.category_id
GROUP BY p2c.product_id, p2c.category_id
HAVING count(*) <= 3
ORDER BY category_id, product_id
You can refer table category to category table and product_to_category to your table name in database.
$category = [0 => 299, 2 => 342, 3 => 134, 4 => 333, 5 => 347, 9 => 296];
$product_to_category = Array
(
0 => Array
(
'product_id' => 23895,
'category_id' => 296,
'relevance' => 77.73432159423828
),
1 => Array
(
'product_id' => 17218,
'category_id' => 296,
'relevance' => 77.73432159423828
),
2 => Array
(
'product_id' => 23896,
'category_id' => 347,
'relevance' => 77.73432159423828
),
3 => Array
(
'product_id' => 23897,
'category_id' => 296,
'relevance' => 77.73432159423828
),
4 => Array
(
'product_id' => 23899,
'category_id' => 296,
'relevance' => 77.73432159423828
),
5 => Array
(
'product_id' => 23894,
'category_id' => 347,
'relevance' => 77.73432159423828
),
6 => Array
(
'product_id' => 23892,
'category_id' => 347,
'relevance' => 77.73432159423828
),
7 => Array
(
'product_id' => 23833,
'category_id' => 347,
'relevance' => 77.73432159423828
)
);
$new_product_to_category = [];
foreach ($category as $categories) {
$count_category_ids = [];
$i=0;
foreach ($product_to_category as $product_to_categories){
if ($categories == $product_to_categories['category_id']){
$count_category_ids[] = $product_to_categories['category_id'];
if (count($count_category_ids) < 4) {
$new_product_to_category[$i]['product_id'] = $product_to_categories['product_id'];
$new_product_to_category[$i]['category_id'] = $product_to_categories['category_id'];
$new_product_to_category[$i]['relevance'] = $product_to_categories['relevance'];
}
}
$i++;
}
}
echo "<pre>";
print_r($new_product_to_category);
Output shows like this :
Array
(
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.734321594238
)
[5] => Array
(
[product_id] => 23894
[category_id] => 347
[relevance] => 77.734321594238
)
[6] => Array
(
[product_id] => 23892
[category_id] => 347
[relevance] => 77.734321594238
)
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.734321594238
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.734321594238
)
[3] => Array
(
[product_id] => 23897
[category_id] => 296
[relevance] => 77.734321594238
)
)
$categories = array(6, 5, 3, 4, 2, 1);
$products = array(
array('product_id' => 231, 'category_id' => 1, 'relevance' => 321),
array('product_id' => 232, 'category_id' => 4, 'relevance' => 322),
array('product_id' => 233, 'category_id' => 2, 'relevance' => 323),
array('product_id' => 234, 'category_id' => 4, 'relevance' => 324),
array('product_id' => 235, 'category_id' => 4, 'relevance' => 325),
array('product_id' => 236, 'category_id' => 2, 'relevance' => 326),
array('product_id' => 237, 'category_id' => 1, 'relevance' => 327),
array('product_id' => 238, 'category_id' => 4, 'relevance' => 328),
array('product_id' => 239, 'category_id' => 1, 'relevance' => 329),
array('product_id' => 240, 'category_id' => 1, 'relevance' => 330)
);
$categoryWiseProduct = array();
foreach ($products as $p) {
foreach ($categories as $c) {
if ($c == $p['category_id']) {
if (isset($categoryWiseProduct[$c]) && count($categoryWiseProduct[$c]) > 2) {
break;
}
$categoryWiseProduct[$c][] = $p;
}
}
}
echo'<pre>';print_r($categoryWiseProduct);die;
Output will be:
Array
(
[1] => Array
(
[0] => Array
(
[product_id] => 231
[category_id] => 1
[relevance] => 321
)
[1] => Array
(
[product_id] => 237
[category_id] => 1
[relevance] => 327
)
[2] => Array
(
[product_id] => 239
[category_id] => 1
[relevance] => 329
)
)
[4] => Array
(
[0] => Array
(
[product_id] => 232
[category_id] => 4
[relevance] => 322
)
[1] => Array
(
[product_id] => 234
[category_id] => 4
[relevance] => 324
)
[2] => Array
(
[product_id] => 235
[category_id] => 4
[relevance] => 325
)
)
[2] => Array
(
[0] => Array
(
[product_id] => 233
[category_id] => 2
[relevance] => 323
)
[1] => Array
(
[product_id] => 236
[category_id] => 2
[relevance] => 326
)
)
)
try this code
$collection = array();
foreach($arr as $key => $value){
if(
isset($collection[$value['category_id']])
&&
count($collection[$value['category_id']]) >= 3
) continue;
$collection[$value['product_id']][] = $value['product_id'];
}
In this way, you will get 3 products from each category.
You can add more condition if you need, like check the product id you can use inside if condition
&& in_array($product_id, $collection[$value['category_id']])
How to add 2nd array into 1st array where [ myid ] matches.
1st Array
Array
(
[0] => Array
(
[myid] => 70
[realname] => Kishore
[full_name] => Kishore Chandra
[category] => professional
[firm_name] => Yes
[designation] => Mechanical
[address] => Dwarakanagar 5th lane
[city] => Vishakhapatnam
[email] => yesapps.india#gmail.com
)
[1] => Array
(
[myid] => 75
[realname] => Vinod kumar
[full_name] => Kishore Chandra
[category] => professional
[firm_name] =>
[designation] =>
[address] =>
[city] =>
[email] => vinod.k.alluri#gmail.com
)
)
2nd Array
Need these projects to be added into Array 1
Array
(
[0] => Array
(
[myid] => 70
[projects] => 20
)
[1] => Array
(
[myid] => 75
[projects] => 43
)
)
I have tried to Merge Array's but no use, i am getting 2 more objects in to this array
I hope my requirement is clear and readable, if not please mention in comment so i could explain it more.
The answer can be in codeigniter also i am using Codeigniter framework
assume that you want the result array to be merging the projects to your first array
you want the result set to be like this
Array
(
[0] => Array
(
[myid] => 70
[realname] => Kishore
[full_name] => Kishore Chandra
[category] => professional
[firm_name] => Yes
[designation] => Mechanical
[address] => Dwarakanagar 5th lane
[city] => Vishakhapatnam
[email] => yesapps.india#gmail.com
[projects] => 20
)
[1] => Array
(
[myid] => 75
[realname] => Vinod kumar
[full_name] => Kishore Chandra
[category] => professional
[firm_name] =>
[designation] =>
[address] =>
[city] =>
[email] => vinod.k.alluri#gmail.com
[projects] => 43
)
)
so try this code
foreach($b as $key => $val){
if(isset($a[$key]) && $a[$key]->myid == $val->myid){
$a[$key]->projects = $val->projects;
}
}
if converted the stdArray to Array
json_decode($a,TRUE)
and json_decode($b, TRUE)
$a = Array
(
0 => Array
(
'myid' => 70,
'realname' => 'Kishore',
'full_name' => 'Kishore Chandra',
'category' => 'professional',
'firm_name' => 'Yes',
'designation' => 'Mechanical',
'address' => 'Dwarakanagar 5th lane',
'city' => 'Vishakhapatnam',
'email' => 'yesapps.india#gmail.com',
),
1 => Array
(
'myid' => 75,
'realname' => 'Vinod kumar',
'full_name' => 'Kishore Chandra',
'category' => 'professional',
'firm_name' => '',
'designation' => '',
'address' => '',
'city' => '',
'email' => 'vinod.k.alluri#gmail.com'
)
);
$b = Array ( 0 => Array(
'myid' => 70,
'projects' => 20
),
1 => Array(
'myid' => 75,
'projects' => 43
)
);
foreach($b as $key => $val){
if(isset($a[$key]) && $a[$key]['myid'] == $val['myid']){
$a[$key]['projects'] = $val['projects'];
}
}
print_r($a);
NOTE:
$b is the 2nd array
$a is the 1st array
foreach($array1 as $key => $array){
foreach($array as $key2 => $value){
$results[$key]['myid'] = $value;
}
}
foreach($array2 as $key => $array){
foreach($array as $key3 => $value){
$results[$key3]['projects'] = $value;
}
}
print_r($results);
I need your help to solve this :)
i've this array
Array (
[0] => Array ( [order_id] => 121 [item_id] => 4344 [item_name] => Product [item_price] => 123 [item_type] => product [paypal_address] => email#test.com [qty] => 4 [currency] => EUR )
[1] => Array ( [order_id] => 121 [item_id] => 3444 [item_name] => Product1 [item_price] => 444 [item_type] => product [paypal_address] => email#test.com [qty] => 2 [currency] => EUR )
[2] => Array ( [order_id] => 121 [item_id] => 1233 [item_name] => Product2 [item_price] => 120 [item_type] => product [paypal_address] => email2#test.com [qty] => 18 [currency] => EUR )
)
I would like to loop on it and group them by values into a new array.
For example:
Pick all items in array that have the same paypal_address sum price, sum qty and move it into new array.
This is what i want to achieve, any tip / suggestion ?
EDIT:
At the end i want an array like this or similar:
Array (
[0] => Array ( [order_id] => 121 [items_id] => array(4344, 3444) [items_name] => 'Product , Product1' [amt] => 567 [item_type] => product [paypal_address] => email#test.com [qty] => 8 [currency] => EUR )
[1] => Array ( [order_id] => 121 [items_id] => 1233 [items_name] => Product2 [amt] => 120 [item_type] => product [paypal_address] => email2#test.com [qty] => 18 [currency] => EUR )
)
EDIT2:
what i did so far. but it doesn't work well and is not good to read.
$groupedParams = array();
foreach($params as $key=>$param){
if(!array_key_exists($param['paypal_address'], $groupedParams) && $param['item_type'] == 'product'){
$groupedParams[$param['paypal_address']] = array(
'order_id' => $param['order_id'],
'item_id' => $param['item_id'],
'item_name' => $param['item_name'],
'qty' => $param['qty'],
'amt' => $param['item_price'],
'item_type' => $param['item_type']
);
}else if(array_key_exists($param['paypal_address'], $groupedParams) && $param['item_type'] == 'product'){
$newItemId = $groupedParams[$param['paypal_address']]['item_id'].','.$param['item_id'];
$newAmt = (int)$groupedParams[$param['paypal_address']]['amt']+(int)$param['item_price'];
$newQty = (int)$groupedParams[$param['paypal_address']]['qty']+(int)$param['qty'];
$newItemName = (string)$groupedParams[$param['paypal_address']]['item_name'].' - '.(int)$param['item_name'];
$groupedParams[$param['paypal_address']] = array(
'order_id' => $param['order_id'],
'item_id' => $newItemId,
'item_name' => $newItemName,
'qty' => $newQty,
'amt' => $newAmt,
'item_type' => $param['item_type']
);
}
}
Thanks
You want to reorganise it to a cleaner format from what I understand, simply put:
$array = array();
foreach($params as $ar) {
$array[$ar['paypal_address']][] = $ar;
}
Example: http://jdl-enterprises.co.uk/sof/25767688.php
Get Array and its values
make unique key on paypal_address
Use unique key to create temp array
Store all values with respective unique key in temp array
$arr = Array (
'0' => Array ( 'order_id' => 121 ,'item_id' => 4344 ,'item_name' => 'Product' ,'item_price' => 123 ,'item_type' => 'product' ,'paypal_address' => 'email#test.com' ,'qty' => 4 ,'currency' => 'EUR' ) ,
'1' => Array ( 'order_id' => 121 ,'item_id' => 3444 ,'item_name' => 'Product1' ,'item_price' => 444 ,'item_type' => 'product' ,'paypal_address' => 'email#test.com' ,'qty' => 2 ,'currency' => 'EUR' ) ,
'2' => Array ( 'order_id' => 121 ,'item_id' => 1233 ,'item_name' => 'Product2' ,'item_price' => 120 ,'item_type' => 'product' ,'paypal_address' => 'email2#test.com' ,'qty' => 18 ,'currency' => 'EUR' )
);
$tmpArr = Array();
$cnt = sizeof($arr);
for($i=0;$i<$cnt;$i++){
$paypal_address = $arr[$i]['paypal_address'];
if(!is_array($tmpArr[$paypal_address])){
$tmpArr[$paypal_address] = Array();
}
$tmpArr[$paypal_address]['paypal_address'] = $arr[$i]['paypal_address'];
$tmpArr[$paypal_address]['order_id'] = $arr[$i]['order_id'];
$tmpArr[$paypal_address]['item_name'][] = $arr[$i]['item_name'];
$tmpArr[$paypal_address]['item_type'][] = $arr[$i]['item_type'];
$tmpArr[$paypal_address]['currency'][] = $arr[$i]['currency'];
$tmpArr[$paypal_address]['qty'] = isset($tmpArr[$paypal_address]['qty']) ? $tmpArr[$paypal_address]['qty'] + $arr[$i]['qty'] : $arr[$i]['qty'];
$tmpArr[$paypal_address]['item_id'] = $arr[$i]['item_id'];
$tmpArr[$paypal_address]['item_id'][] = $item_id;
$tmpArr[$paypal_address]['item_price'] = isset($tmpArr[$paypal_address]['item_price']) ? $tmpArr[$paypal_address]['item_price'] + $arr[$i]['item_price'] : $arr[$i]['item_price'];
}
print_r($tmpArr);
Output of the array is as below:
Array([email#test.com] => Array ([paypal_address] => email#test.com[order_id] => 121[item_name] => Array ( [0] => Product [1] => Product1 )[item_type] => Array ( [0] => product [1] => product )[currency] => Array ( [0] => EUR [1] => EUR )[qty] => 6[item_id] => 3444[item_price] => 567 )[email2#test.com] => Array ([paypal_address] => email2#test.com[order_id] => 121[item_name] => Array ( [0] => Product2 )[item_type] => Array ( [0] => product )[currency] => Array ( [0] => EUR )[qty] => 18[item_id] => 1233[item_price] => 120 ))
Note: Make changes in code according to your requirement
$details =Array (
[0] => Array ( [order_id] => 121 [item_id] => 4344 [item_name] => Product [item_price] => 123 [item_type] => product [paypal_address] => email#test.com [qty] => 4 [currency] => EUR )
[1] => Array ( [order_id] => 121 [item_id] => 3444 [item_name] => Product1 [item_price] => 444 [item_type] => product [paypal_address] => email#test.com [qty] => 2 [currency] => EUR )
[2] => Array ( [order_id] => 121 [item_id] => 1233 [item_name] => Product2 [item_price] => 120 [item_type] => product [paypal_address] => email2#test.com [qty] => 18 [currency] => EUR )
)
$sorted = array();
foreach ($details as $key => $value) {
$add = $value['paypal_address'];
foreach ($details as $index => $detail) {
if ($add == $detail['paypal_address']) {
foreach ($detail as $cat => $val) {
if (!in_array($val, $sorted[$key][$cat])) {
$sorted[$key][$cat][] = $val;
}
}
unset($details[$index]);
}
}
}
// Output array
print_r($sorted);