I have two multidimensional associative arrays with differen items count.
Important is that I don't know which aray will have more elements (A or B)
First array (A):
[0] => Array
(
[catID] => 65
[discount] => 10
[productID] => Array
(
[0] => 10887
[1] => 8508
[2] => 8350
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
[1] => Array
(
[catID] => 66
[discount] => 10
[productID] => Array
(
[0] => 13184
[1] => 10707
[2] => 8350
)
[startDate] => 10/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Second array (B):
[0] => Array
(
[catID] => 72
[discount] => 15
[productID] => Array
(
[0] => 16239
[1] => 16236
[2] => 10887
[3] => 13184
[4] => 8524
[5] => 13314
)
[startDate] => 12/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
After compare these arrays (A, B) I'd like to retrive something like that:
Array A(remove productID if exists in array B):
[0] => Array
(
[catID] => 65
[discount] => 10
[productID] => Array
(
[1] => 8508
[2] => 8350
)
[startDate] => 05/12/2022 12:00 am
[endDate] => 10/12/2022 12:00 am
)
[1] => Array
(
[catID] => 66
[discount] => 10
[productID] => Array
(
[0] => 10707
)
[startDate] => 10/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Array B(no changes):
[0] => Array
(
[catID] => 72
[discount] => 15
[productID] => Array
(
[0] => 16239
[1] => 16236
[2] => 10887
[3] => 13184
[4] => 8524
[5] => 13314
)
[startDate] => 12/12/2022 12:00 am
[endDate] => 15/12/2022 12:00 am
)
Populate a flat "blacklist" array from your second array.
Loop over the first array's rows and filter the productId values against the blacklist array.
Code: (Demo)
$blacklist = array_merge(...array_column($b, 'productID'));
var_export(
array_map(
fn($row) => array_replace($row, ['productID' => array_values(array_diff($row['productID'], $blacklist))]),
$a
)
);
Or if you don't mind a classic foreach(), then this may be easier to read: (Demo)
$blacklist = array_merge(...array_column($b, 'productID'));
foreach ($a as &$row) {
$row['productID'] = array_values(array_diff($row['productID'], $blacklist));
}
var_export($a);
Related
I have 2 arrays. I would like to merge them into 1 array but with a condition. If Ref and Id are equal then merge
Array Number One
(
[0] => Array
(
[Id] => 60
[Name] => Water Authority
)
[1] => Array
(
[Id] => 63
[Name] => Service Station
)
Array Number Two
(
[0] => Array
(
[Date] => 2017-10-12
[Amount] => 130.00
[Ref] => 60
[Description] => Water Bill Oct
)
[1] => Array
(
[Date] => 2017-10-10
[Amount] => 130.00
[Ref] => 63
[Description] => Gas
)
[2] => Array
(
[Date] => 2017-09-17
[Amount] => 600.00
[Ref] => 60
[Description] => Water bill Sept
)
I would like to merge them with so I end up with:
Merged Array
(
[0] => Array
(
[Date] => 2017-10-12
[Amount] => 130.00
[Ref] => 60
[Description] => Water Bill Oct
[Id] => 60
[Name] => Water Authority
)
[1] => Array
(
[Date] => 2017-10-10
[Amount] => 130.00
[Ref] => 63
[Description] => Gas
[Id] => 63
[Name] => Service Station
)
and so on...
How would I do that? I tried array_merge but I know it needs something else, just don't know what!
Let assume first array be as $array1, second array be array2
$result = [];
foreach ($array1 as $key1 => $value1) {
foreach ($array2 as $key2 => $value2) {
if ($value1['Id']==$value2['Ref']) {
$result[]=$value2+$value1;
}
}
}
The result will be as you expected
This is the original main array:
Array
(
[0] => Array
(
[subtotal] => 0.6000
[taxes] => 0.0720
[charged_amount] => 0.6720
[total_discount] => 0.0000
[provinceName] => BC
[store_key] => 1
[store_id] => 5834
[categories] => Array
(
[2] => 0.6000
[4] => 0
[3] => 0
)
)
[1] => Array
(
[subtotal] => 29.8500
[taxes] => 2.3270
[charged_amount] => 20.2370
[total_discount] => 11.9400
[provinceName] => MB
[store_key] => 9
[store_id] => 1022
[categories] => Array
(
[2] => 0
[4] => 29.8500
[3] => 0
)
)
[2] => Array
(
[subtotal] => 0.3000
[taxes] => 0.0390
[charged_amount] => 0.3390
[total_discount] => 0.0000
[provinceName] => NB
[store_key] => 8
[store_id] => 1013
[categories] => Array
(
[2] => 0.3000
[4] => 0
[3] => 0
)
)
[3] => Array
(
[subtotal] => 24.3100
[taxes] => 1.1830
[charged_amount] => 10.2830
[total_discount] => 15.2100
[provinceName] => NL
[store_key] => 4
[store_id] => 3033
[categories] => Array
(
[2] => 24.3100
[4] => 0
[3] => 0
)
)
[4] => Array
(
[subtotal] => 1116.3400
[taxes] => 127.6960
[charged_amount] => 1110.0060
[total_discount] => 134.0300
[provinceName] => ON
[store_key] => 2
[store_id] => 1139
[categories] => Array
(
[2] => 85.7300
[4] => 143.2800
[3] => 887.3300
)
)
[5] => Array
(
[subtotal] => 10.8500
[taxes] => 1.4100
[charged_amount] => 12.2600
[total_discount] => 0.0000
[provinceName] => ON
[store_key] => 5
[store_id] => 1116
[categories] => Array
(
[2] => 10.8500
[4] => 0
[3] => 0
)
)
)
I just need to add the values of the array [categories] with same keys and use it further to print the total, but not getting correct output, can someone help me out to get the desired result:
Desired result
An array with same keys but total of individual array values
Array ( [2] => 0.9000 [4] => 29.8500 [3] => 1.5 )
NOTE: Initial array is dynamic can have n number of key value pair
Thanks
The first thing that you need to do is iterate through the outer array. Then, for each row in the outer array, you and to iterate through each entry in the category element. So this means that we have two foreach loops. Inside the inner foreach, we simply set the value for the current index to be the value of the same index on a 'sum' array (if it doesn't already exist), or increment the value of that index if it already exists in the 'sum' array.
<?php
$sumArray = array();
foreach($outerArray as $row)
{
foreach($row["categories"] as $index => $value)
{
$sumArray[$index] = (isset($sumArray[$index]) ? $sumArray[$index] + $value : $value);
}
}
?>
Demo using your example array
I have two array I want to match ['Name'] value of second array [Seltemgr] value, if ['Name']=>value == [Seltemgr]=>value then <input type="checkbox" checked="checked"> else unchecked, Is this possible to match two multidimensional array having different no of element with different keys.
Array
(
[0] => Array
(
[tnid] => 45
[Name] => Financial Tips
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/5
)
[1] => Array
(
[tnid] => 42
[Name] => Products
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/2
)
[2] => Array
(
[tnid] => 44
[Name] => Health Tips
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/4
)
[3] => Array
(
[tnid] => 43
[Name] => Personal Events
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/3
)
[4] => Array
(
[tnid] => 41
[Name] => Calendar
[Email] => Array
[Href] => http://amt-ars-d.sevenverbs.com/api/v1/emailtemplategroups/1
)
)
Array
(
[0] => Array
(
[emarketid] => 77
[agentid] => 81
[customerid] => 16901
[Seltemgr] => Calendar
[seltemname] =>
)
[1] => Array
(
[emarketid] => 78
[agentid] => 81
[customerid] => 16901
[Seltemgr] => Financial Tips
[seltemname] =>
)
[2] => Array
(
[emarketid] => 79
[agentid] => 81
[customerid] => 16901
[Seltemgr] => Merry Christmas
[seltemname] =>
)
[3] => Array
(
[emarketid] => 80
[agentid] => 81
[customerid] => 16901
[Seltemgr] => Drip financial tip 3
[seltemname] =>
)
)
$match = [];
foreach ($array1 as $k1=>$a1){
foreach($array2 as $k2=>$a2){
if($a2['Seltemgr'] == $a1['Name']){
$match[] = array($k1,$k2);
}
}
}
So $match contains the index of each array where the Name and Seltemgr match. Not saying this is the optimal way to do this but The intersect functions often require keys to match up.
I have a dataset similar to this in which I am trying to replace the numeric key values within DATA to the corresponding values in COLUMNS. I can do this in a loop but I don't think I'm doing it in the most efficient way possible. Can anyone suggest any nice functions that I haven't considered to accomplish this?
Existing Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[0] => 141627
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 18
[4] => 11
[5] => Final
)
[1] => Array
(
[0] => 140895
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 16
[4] => 10
[5] => Semi-Final
)
)
)
Desired Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[MATCHID] => 141627
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 18
[ROUNDSORT] => 11
[ROUNDNAME] => Final
)
[1] => Array
(
[MATCHID] => 140895
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 16
[ROUNDSORT] => 10
[ROUNDNAME] => Semi-Final
)
)
)
foreach ($data->DATA as $key => $array) {
$data->DATA[$key] = array_combine($data->COLUMNS, $array);
}
$data is the object you showed.
Loop trough the data and combine the keys with the data, see array_combine
$data->DATA = array_map(function (array $entry) use ($data) {
return array_combine($data->COLUMNS, $entry);
}, $data->DATA);
I'm afraid that it is simply not possible what I'm trying to do, but I hope you can help me to find a nice way to solve this problem.
I've got the following PHP-array:
Array (
[0] => Array ( [article] => 10.499-1 [operation] => KN_KABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[1] => Array ( [article] => 10.499-1 [operation] => LAS_LABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[2] => Array ( [article] => 10.499-1 [operation] => ASS_HARNES [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[3] => Array ( [article] => 10.499-1 [operation] => CONTROLE [date] => 07-06-2013 [hours] => 0 [quantity] => 1 )
[4] => Array ( [article] => 24.030 [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[5] => Array ( [article] => 24.030 [operation] => ZAGEN-RAIL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[6] => Array ( [article] => 24.030 [operation] => KN_KABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[7] => Array ( [article] => 24.030 [operation] => ASS_RAIL [date] => 05-06-2013 [hours] => 0 [quantity] => 1 )
[8] => Array ( [article] => 791 070-6/GS/P [operation] => GS_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[9] => Array ( [article] => 791 070-6/GS/P [operation] => PR_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[10] => Array ( [article] => 791 070-6/GS/P [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[11] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => MAGAZIJN [date] => 10-06-2013 [hours] => 0 [quantity] => 1 )
[12] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => PR_UNIT [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
[13] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => LAB_PLAKKE [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
)
What I'm trying to do is to count for each date (key "datum") the sum of hours (in this example they are all 0 but I still want to do this because this will change in the future). What would be very practicle is to do a query like sql like SELECT SUM(hours) GROUP BY date but this is no SQL unfortunately.
Is there a way to group (and order) my array by a specific key (in this case "date") or, if not, is there an other way to get the result what I want?
EDIT
I recently added a key "department" which should be grouped by to. Thereby I do not only want to count the sum of "hours", but "quantity" too
Just create simply foreach for this.
$arr = array();
$sums = array();
foreach($arr as $k=>$v)
{
if(!isset($sums[$v['date']][$v['department']]['hours'])) $sums[$v['date']][$v['department']]['hours'] = 0;
if(!isset($sums[$v['date']][$v['department']]['quantity'])) $sums[$v['date']][$v['department']]['quantity'] = 0;
$sums[$v['date']][$v['department']]['hours'] += $v['hours'];
$sums[$v['date']][$v['department']]['quantity'] += $v['quantity'];
}
print_r($sums);
it will create array $sum where keys are your dates. If value doesn't exists it will add the value of hours to 0, else if it exists it will add to existing value.
edit
Fitted to OP needs.