combine array using child index value - php

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

Related

PHP Subtract multidimensional arrays based on 2 values

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

How to map multidimensional array

I have an array of arrays that I need to consolidate into another array.
I have tried mapping over it, matching object_id, and gathering all account_ids for said object_id, but all my attempts are not even close as I am trying to learn PHP
This is the original array
[0] => Array
(
[rank] => 0
[id] => 6
[object_id] => 3
[account_id] => 13
)
[1] => Array
(
[rank] => 1
[id] => 7
[object_id] => 3
[account_id] => 565
)
[2] => Array
(
[rank] => 2
[id] => 1823
[object_id] => 825
[account_id] => 563
)
[3] => Array
(
[rank] => 3
[id] => 1824
[object_id] => 825
[account_id] => 564
)
[4] => Array
(
[rank] => 4
[id] => 1825
[object_id] => 825
[account_id] => 565
)
[5] => Array
(
[rank] => 5
[id] => 7187
[object_id] => 3113
[account_id] => 564
)
[6] => Array
(
[rank] => 6
[id] => 7188
[object_id] => 3113
[account_id] => 565
)
This is the desired result
[3] => [13, 565],
[825] => [563, 564, 565],
[3113] => [564, 565],
You need to create a new array by using object_id index.
Example:
<?
$array = array(
array('rank'=>0,'id'=>6,'object_id'=>3,'account_id'=>13),
array('rank'=>1,'id'=>7,'object_id'=>3,'account_id'=>565),
array('rank'=>2,'id'=>1823,'object_id'=>825,'account_id'=>563),
array('rank'=>3,'id'=>1824,'object_id'=>825,'account_id'=>564),
array('rank'=>4,'id'=>1825,'object_id'=>825,'account_id'=>565),
array('rank'=>5,'id'=>7187,'object_id'=>3113,'account_id'=>564),
array('rank'=>6,'id'=>7188,'object_id'=>3113,'account_id'=>565),
);
$newArray = array(); // initiliaze array
foreach ($array as $key => $value) {
$newArray[$value['object_id']][] = $value['account_id']; // save it in group
}
echo "<pre>";
print_r($newArray); // result
?>
Running Example

extracting an element of an array [duplicate]

This question already has answers here:
Get index of row with qualifying value from a 2d array
(5 answers)
Closed 7 years ago.
Assume this array:
Array ( [0] => Array ( [id] => 1171 [product_id] => 140 [fileid] => 479717 [purchid] => 847 [cartid] => 833 [uniqueid] => f100c3b3a853202fb6559fbacf025a6aa07f52c7 [downloads] => 99998 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[1] => Array ( [id] => 1172 [product_id] => 140 [fileid] => 313624 [purchid] => 847 [cartid] => 833 [uniqueid] => f00a3c91378ad469f333abeec64753b275f10670 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[2] => Array ( [id] => 1173 [product_id] => 140 [fileid] => 313618 [purchid] => 847 [cartid] => 833 [uniqueid] => ac125595e2dbca6a086261434582f6e7dfc5638e [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 )
[3] => Array ( [id] => 1174 [product_id] => 140 [fileid] => 313526 [purchid] => 847 [cartid] => 833 [uniqueid] => 3e6123e0a4453de71dec91a177f5b34217625680 [downloads] => 99999 [ip_number] => [active] => 1 [datetime] => 2015-06-02 20:10:05 ))
I want to "extract" the [0] array and use it for something else BUT it has to be conditioned.
I said the 0 element because it has the fileid = 479717 the one that i want.
So i`m looking at extracting the array (in my case [0]) that has filed = $myvalue. Where i can set $myvalue to whatever i want.
Does this fits your needs ?
function findById($id, $arrayOfArrays){
foreach ( $arrayOfArrays as $contents )
if ( $contents['id'] == $id ) return $contents;
}
function filter($fileid, array $array) {
foreach ($array as $key => $value) {
if ($value['fileid'] === $fileid) {
return $value;
}
}
}

Array Merge Or Combine Issue

I have two array Array1 and Array2 and now i want to combine or you can say merge these array into one according to product_option_id in each array means if product_option_id is same then push the second array data into one.
my First array is :
Array
(
[0] => Array
(
[DispensaryInventory] => Array
(
[id] => 21
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 1
)
)
[1] => Array
(
[DispensaryInventory] => Array
(
[id] => 22
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 2
)
)
[2] => Array
(
[DispensaryInventory] => Array
(
[id] => 23
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 3
)
)
[3] => Array
(
[DispensaryInventory] => Array
(
[id] => 24
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 4
)
)
[4] => Array
(
[DispensaryInventory] => Array
(
[id] => 25
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 5
)
)
)
and second array is
Array
(
[0] => Array
(
[DriverInventory] => Array
(
[id] => 21
[driver_id] => 10
[dispensary_inventory_id] => 12
[product_id] => 7
[product_option_id] => 2
)
[Mydata] => Array
(
[price]= 2545
)
)
[1] => Array
(
[DriverInventory] => Array
(
[id] => 22
[driver_id] => 10
[dispensary_inventory_id] => 13
[product_id] => 7
[product_option_id] => 3
[quantity] => 16
)
[Mydata] => Array
(
[price]= 15987
)
)
[2] => Array
(
[DriverInventory] => Array
(
[id] => 23
[driver_id] => 10
[dispensary_inventory_id] => 14
[product_id] => 7
[product_option_id] => 4
[quantity] => 20
)
[Mydata] => Array
(
[price]= 96744
)
)
[3] => Array
(
[DriverInventory] => Array
(
[id] => 24
[driver_id] => 10
[dispensary_inventory_id] => 15
[product_id] => 7
[product_option_id] => 5
[quantity] => 45
)
[Mydata] => Array
(
[price]= 97455
)
)
)
finally i want to be a result as
Array
(
[0] => Array
(
[DispensaryInventory] => Array
(
[id] => 21
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 1
)
)
[1] => Array
(
[DispensaryInventory] => Array
(
[id] => 22
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 2
)
[Mydata] => Array
(
[price]= 2545
)
)
[2] => Array
(
[DispensaryInventory] => Array
(
[id] => 23
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 3
)
[Mydata] => Array
(
[price]= 15987
)
)
[3] => Array
(
[DispensaryInventory] => Array
(
[id] => 24
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 4
)
[Mydata] => Array
(
[price]= 96744
)
)
[4] => Array
(
[DispensaryInventory] => Array
(
[id] => 25
[dispensary_id] => 18
[product_id] => 7
[product_option_id] => 5
)
[Mydata] => Array
(
[price]= 97455
)
)
)
Try this.
foreach ($array1 as $key => $value)
{
foreach ($array2 as $key1 => $value1)
{
if($value['DispensaryInventory']['product_option_id']==$value1['DriverInventory']['product_option_id'])
{
$price_data[$key]['Mydata'] = $value1['Mydata'];
}
}
}

rearrange multidimensional array on basis of value of inner array

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

Categories