PHP Subtract multidimensional arrays based on 2 values - php

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

Related

PHP: Find elements in deep array, then modify each parent element

I have a varying level of deepness for a multidimensional array. I want to find the elements where UserId isset (see example array below), then add a key/value pair ([show] => true) to the matched element and each parent array. There may be multiple matches that will need to modify the parents.
I have this:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[ownerEntityId] => 2
[children] => Array
(
[0] => Array
(
[id] => 15
[parent_id] => 3
[ownerEntityId] => 14
[children] => Array
(
[0] => Array
(
[id] => 17
[parent_id] => 15
[ownerEntityId] =>
[userId] => 2
)
[1] => Array
(
[id] => 18
[parent_id] => 15
[ownerEntityId] =>
)
[2] => Array
(
[id] => 19
[parent_id] => 15
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 11
[parent_id] => 3
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 26
[parent_id] => 1
[ownerEntityId] =>
[children] => Array
(
[0] => Array
(
[id] => 23
[parent_id] => 26
[ownerEntityId] => 24
[1] => Array
(
[id] => 41
[parent_id] => 26
[ownerEntityId] =>
)
)
)
I want this:
Array
(
[0] => Array
(
[id] => 3
[parent_id] => 0
[ownerEntityId] => 2
[show] => true //***Added
[children] => Array
(
[0] => Array
(
[id] => 15
[parent_id] => 3
[ownerEntityId] => 14
[show] => true //***Added
[children] => Array
(
[0] => Array
(
[id] => 17
[parent_id] => 15
[ownerEntityId] =>
[show] => true //***Added
[userId] => 2
)
[1] => Array
(
[id] => 18
[parent_id] => 15
[ownerEntityId] =>
)
[2] => Array
(
[id] => 19
[parent_id] => 15
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 11
[parent_id] => 3
[ownerEntityId] =>
)
)
)
[1] => Array
(
[id] => 26
[parent_id] => 1
[ownerEntityId] =>
[children] => Array
(
[0] => Array
(
[id] => 23
[parent_id] => 26
[ownerEntityId] => 24
[1] => Array
(
[id] => 41
[parent_id] => 26
[ownerEntityId] =>
)
)
)
I have been messing with multiple recursion functions that have failed me miserably.
https://ideone.com/HoJPry
function hasUserId (&$el) {
if(isset($el['children'])) {
$ret = false;
foreach($el['children'] as &$child) {
if(hasUserId($child)) {
$ret=true;
}
}
if($ret) {
$el['show']=true;
return true;
}
} ;
if (isset($el['user_id'])) {
$el['show']=true;
return true;
}
}
foreach($top as $i => $t)
{
foreach($t['children'] as $n => $mid)
{
foreach($mid['children'] as $x => $bottom)
{
if($bottom['id'] !== $target_id)
continue;
$top[$i]['show'] = true;
$top[$i]['children'][$n]['show'] = true;
$top[$i]['children'][$n]['children'][$x]['show'] = true;
}
}
}

convert this array format to single array in php

I want to convert this array in a single dimensional flat array without losing the sort order.
Array
(
[0] => Array
(
[id] => 1
[title] => Computer
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => keyboard
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[title] => Mouse
[parent_id] => 4
[children] => Array
(
[0] => Array
(
[id] => 7
[title] => webcam
[parent_id] => 6
)
)
)
)
)
)
)
[1] => Array
(
[id] => 43
[title] => Mobile
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 5
[title] => bar phones
[parent_id] => 43
)
[1] => Array
(
[id] => 47
[title] => Touchscreen
[parent_id] => 43
[children] => Array
(
[0] => Array
(
[id] => 41
[title] => Samsung
[parent_id] => 47
)
[1] => Array
(
[id] => 44
[title] => Micromax
[parent_id] => 47
)
[2] => Array
(
[id] => 45
[title] => Huawei
[parent_id] => 47
)
)
)
)
)
[2] => Array
(
[id] => 46
[title] => Camera
[parent_id] => 0
)
[3] => Array
(
[id] => 42
[title] => Heater
[parent_id] => 0
)
)
Give it try with below function:
function makeOneDimensionArray(array $array, &$res = array())
{
foreach($array as $arr)
{
$res[] = array(
'id' => $arr['id'],
'title' => $arr['title'],
'parent_id' => $arr['parent_id']
);
if(isset($arr['children']))
{
makeOneDimensionArray($arr['children'], $res);
}
}
return $res;
}
$finalArr = makeOneDimensionArray($your_array);
print_r($finalArr);

How to reformat an array using php?

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.

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

in_array still gets the company id even though I put a check

I have the following code for the array below
$arrCompany = array();
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany)){
$arrCompany['company'][] = $user['User']['company_id'];
}else{}
}
what I am trying to do is just have one entery of company id so I can add users under it but for some reasons its not working
here is my array
Array
(
[0] => Array
(
[User] => Array
(
[id] => 1
[company_id] => 20
[type] =>
)
)
[1] => Array
(
[User] => Array
(
[id] => 6
[company_id] => 21
[type] =>
)
)
[2] => Array
(
[User] => Array
(
[id] => 7
[company_id] => 22
[type] =>
)
)
[3] => Array
(
[User] => Array
(
[id] => 14
[company_id] => 21
[type] =>
)
)
[4] => Array
(
[User] => Array
(
[id] => 15
[company_id] => 22
[type] =>
)
)
[5] => Array
(
[User] => Array
(
[id] => 16
[company_id] => 21
[type] =>
)
)
)
)
when I do var_dump...i get this
Array
(
[company] => Array
(
[0] => 20
[1] => 21
[2] => 22
[3] => 21
[4] => 22
[5] => 21
)
)
you can see the company has been repeated
$arrCompany = array('company'=>array());
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany['company'])){
$arrCompany['company'][] = $user['User']['company_id'];
}
}

Categories