i have an array like this
Array
(
[0] => Array
(
[cat_name] => Clothing
[cat_id] => 1
[item_name] => shirt
[item_id] => 1
[src] => 177
[sic] => 78
)
[1] => Array
(
[cat_name] => Stationary
[cat_id] => 3
[item_name] => note book
[item_id] => 8
[src] => 50
[sic] => 10
)
[2] => Array
(
[cat_name] => Stationary
[cat_id] => 3
[item_name] => ball pen
[item_id] => 10
[src] => 59
[sic] => 58
)
[3] => Array
(
[cat_name] => Expandable
[cat_id] => 4
[item_name] => vim powder
[item_id] => 14
[src] => 34
[sic] => 23
)
[4] => Array
(
[cat_name] => Clothing
[cat_id] => 1
[item_name] => pant
[item_id] => 16
[src] => 100
[sic] => 10
)
)
now what i want
first it sorted by cat_id and then a create a new array having below structure
Array
(
[0] =>"Clothing"=>Array
(
[0]=>Array
(
[item_name] => shirt
[item_id] => 1
[src] => 177
[sic] => 78
)
[1] => Array
(
[item_name] => pant
[item_id] => 16
[src] => 100
[sic] => 10
)
)
[1] => "Stationary"=>Array
(
[0] => Array
(
[item_name] => note book
[item_id] => 8
[src] => 50
[sic] => 10
)
[1] => Array
(
[item_name] => ball pen
[item_id] => 10
[src] => 59
[sic] => 58
)
)
[2]=>"Expandable => Array
(
[0] => Array
(
[item_name] => vim powder
[item_id] => 14
[src] => 34
[sic] => 23
)
)
)
Untested
$output = array();
foreach($array as $item) {
if(!isset($output[$item['cat_name']])) {
$output[$item['cat_name']] = array();
}
$catName = $item['cat_name'];
unset($item['cat_name']);
$output[$catName][] = $item;
}
function cmp($a, $b)
{
if ($a['cat_id'] == $b['cat_id']) {
return 0;
}
return ($a['cat_id'] < $b['cat_id']) ? -1 : 1;
}
// sort by cat_id
usort($array, 'cmp');
// create the grouped array
$res = array();
foreach($array as &$item) {
$cat_name = $item['cat_name'];
unset($item['cat_name'], $item['cat_id']);
$res[$cat_name][] = $item;
}
Related
I need to subtract the qt from two arrays based on id and type, keeping the array complete.
How do I do in php to be able to subtract these arrays?
I have 2 arrays:
=> this is the first array with multiple key "down"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => down
[qt] => 12
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => down
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => down
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => down
[qt] => 45
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => down
[qt] => 3
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
=> this is the second array with multiple key "up"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => up
[qt] => 5
)
[1] => Array
(
[id] => 86
[loc] => 3
[type] => up
[qt] => 27
)
[2] => Array
(
[id] => 23
[loc] => 9
[type] => up
[qt] => 3
)
)
=> I need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => total
[qt] => 7
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => total
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => total
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => total
[qt] => 18
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => total
[qt] => 0
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
Try this out
function findMatch($array, $id, $loc)
{
foreach ($array as $key => $item) {
if ($item["id"] == $id and $item["loc"] == $loc) {
return $key;
}
}
return false;
}
$total = [];
foreach($down as $d) {
$matched = findMatch($up, $d["id"], $d["loc"]);
if($matched !== false){
$total[] = array_replace($d, [
"type" => "total",
"qt" => ($d["qt"] - $up[$matched]["qt"])
]);
} else {
$total[] = array_replace($d, ["type" => "total"]);
}
}
print_r($total);
I have 2 arrays like:
$arr1 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
)
$arr2 = [220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
I need the result like :
$arr3 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
[220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
For this I simply using array_merge
$arr3= $arr1+ $arr2;
But after that I got the result like:
$arr3 = [230] => Array
(
[itemid] => 230
[name] => test1
[category] => toy
[price] => 10.00
)
[240] => Array
(
[itemid] => 240
[name] => test2
[category] => toy
[price] => 8.00
)
[220] => Array
(
[itemid] => 220
[name] => test4
[category] => toy
[price] => 20.00
)
[225] => Array
(
[itemid] => 225
[name] => test5
[category] => battery
[price] => 4.00
)
[248] => Array
(
[itemid] => 248
[name] => test6
[category] => book
[price] => 3.00
)
[245] => Array
(
[itemid] => 245
[name] => test3
[category] => pen
[price] => 5.00
)
[236] => Array
(
[itemid] => 236
[name] => test7
[category] => pen
[price] => 2.00
)
)
My issue is after merging 2 associative arrays with different keys, I got the first array and second array mixed which means I need the result like the first three array elements of the first array, after that 4 array elements of the second array.
Can you anyone help me, please, It will be helpful for me
You could use:
foreach($arr2 as $key => $record) {
$arr1[$key] = $record;
}
This would result in the order you're looking for.
demo
I have following array output.i want to combine them according to tax class id Please check the below array and another array format which i want is 2nd array
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-S
[qty_ordered] => 5
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-M
[qty_ordered] => 9
)
[2] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-L
[qty_ordered] => 9
)
[3] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-XL
[qty_ordered] => 9
)
I want to combine array those have same tax_class_id like below
[0] => Array(
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-S
[qty_ordered] => 5
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-L
[qty_ordered] => 9
)
)
[1] => Array(
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-M
[qty_ordered] => 9
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-XL
[qty_ordered] => 9
)
)
How can i get array in above format. where sub array has same tax_class_id.
Solution....
foreach($array as $row){
$new[$row['tax_class_id']][] = $row;
}
echo "<pre>";print_r($new);
Result
Array
(
[18] => Array
(
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-S
[qty_ordered] => 5
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 18
[sku] => 620068-429-L
[qty_ordered] => 9
)
)
[28] => Array
(
[0] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-M
[qty_ordered] => 9
)
[1] => Array
(
[id] => 1947
[cat_id] => 48
[tax_class_id] => 28
[sku] => 620068-429-XL
[qty_ordered] => 9
)
)
)
You could do a combination of "usort()" and creating a new array via loop from the resulting usort() array.
usort() example to get you started (based on Sort multi-dimensional array by specific key )
<!DOCTYPE html>
<html>
<body>
<?php
$age = array(
array(
"id"=>"1",
"cat_id"=>"11",
"text_id"=>"43"
),
array(
"id"=>"2",
"cat_id"=>"22",
"text_id"=>"22"
),
array(
"id"=>"3",
"cat_id"=>"33",
"text_id"=>"43"
),
array(
"id"=>"4",
"cat_id"=>"44",
"text_id"=>"43"
),
array(
"id"=>"5",
"cat_id"=>"55" ,
"text_id"=>"22"
)
);
function cmp($a, $b)
{
return strcmp($a['text_id'], $b['text_id']);
}
usort($age, "cmp");
// Just to show that the array has been sorted
echo '<pre>';
print_r($age);
echo '</pre>';
// Create new array using loops here
//......
?>
</body>
</html>
You can do a loop like this:
$val = [];
$newArray = []
foreach ($products as $product) {
$key = array_search($product['tax_class_id'],$val);
if(!$key) {
$val[] = $product['tax_class_id'];
$key = array_search($product['tax_class_id'],$val);
}
$newArray[$key][] = $product;
}
demo:https://ideone.com/OhRieF#stdin
I have the following array:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 50
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 2
[category_id] => 2
[product_id] => 2
[amount] => 15
[cost] => 3000
[comment] =>
[created] => 2015-06-22 18:10:36
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[2] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
I want to remove duplicate value of same product_id inside [Import][product_id] but want sum [Import][amount]. My expected array is:
Array
(
[0] => Array
(
[Import] => Array
(
[id] => 1
[category_id] => 2
[product_id] => 2
[amount] => 65
[cost] => 8320
[comment] => transportation and others cost: 100
[created] => 2015-06-23 19:21:10
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 1
[confirmed] => 2
[canceled] => 0
)
)
[1] => Array
(
[Import] => Array
(
[id] => 3
[category_id] => 2
[product_id] => 1
[amount] => 15
[cost] => 2000
[comment] =>
[created] => 2015-06-23 19:20:15
)
[0] => Array
(
[total_sell] => 10
[no_contact] => 0
[confirmed] => 0
[canceled] => 0
)
)
)
It will be really a gift if anyone give a function to solve this issue.
$filteredArray = [];
foreach ($array as $productData) {
if (isset($filteredArray[$productData['Import']['product_id']])) {
$filteredArray[$productData['Import']['product_id']]['Import']['amount'] += $productData['Import']['amount'];
}
else {
$filteredArray[$productData['Import']['product_id']] = $productData;
}
}
print_r($filteredArray);
Ah.. forgot to mention - $array is Your base array.
/**
* Removes duplicate summing amount from products array
*
* #param array $array
* #return array
*/
function removeDuplicates($array)
{
$idsCount = array();
foreach ($array as $key => $value) {
$idsCount[$value['Import']['product_id']]['count'] += 1;
$idsCount[$value['Import']['product_id']]['sum'] += $value['Import']['amount'];
if ($idsCount[$value['Import']['product_id']]['count'] > 1) {
unset($array[$key]);
$array[$idsCount[$value['Import']['product_id']]['key']]['Import']['amount'] = $idsCount[$value['Import']['product_id']]['sum'];
} else {
$idsCount[$value['Import']['product_id']]['key'] = $key;
}
}
return $array;
}
i know it looks crazy but is formatted on your specific array.
please someone will guide me to how to add the values of below conditions and then update the added value in code .
how to sum value of where code =low_order_fee and where code= goods_total then update the sum value in where code=goods_total.
Array
(
[0] => Array
(
[order_total_id] => 999
[order_id] => 194
[code] => goods_total
[title] => Goods-Total
[text] => £130.00
[value] => 130.0000
[sort_order] => 1
)
[1] => Array
(
[order_total_id] => 1000
[order_id] => 194
[code] => low_order_fee
[title] => * Carriage
[text] => £10.00
[value] => 10.0000
[sort_order] => 3
)
[2] => Array
(
[order_total_id] => 1001
[order_id] => 194
[code] => sub_total
[title] => Sub-Total
[text] => £130.00
[value] => 130
[sort_order] => 4
)
[3] => Array
(
[order_total_id] => 1002
[order_id] => 194
[code] => tax
[title] => VAT (20%)
[text] => £26.00
[value] => 26.0000
[sort_order] => 5
)
[4] => Array
(
[order_total_id] => 1003
[order_id] => 194
[code] => total
[title] => Invoice Total
[text] => £166.00
[value] => 166.0000
[sort_order] => 9
)
)
A rudimentary solution:
foreach ($array_var as $key => $item) {
if ($item['code'] == 'low_order_fee') {
$first_val = $item['value'];
}
if ($item['goods_total'] == 'low_order_fee') {
$sec_val = $item['value'];
$position = $key;
}
}
$array_var[$position]['goods_total'] = $first_val + $sec_val;
But maybe you should think about store the values in another way to make easier access to them.
I hope it helps.