PHP merge arrays with a condition - php

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

Related

How to Compare two multidimensional associative arrays with differen items count

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

Rearrange items in multidimensional array

I have a multidimensional array, I want to take the key 'data' from each array item and form another array, like first array 'data' element has 3 items, second has 1 item, 3rd and 4th are empty, and 5th has one item, I want to make a separate array like $temp_array['first_item_from_first_array,..., first_item_from_fifth_array, 'second_item_from_second_array,....,second_item_From_fifth_array]
input array is,
Array
(
[0] => Array
(
[meal_type] => bf
[label] => Breakfast
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
)
)
[1] => Array
(
[meal_type] => sn
[label] => Snacks
[calorie_limit] => 10
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
)
)
[2] => Array
(
[meal_type] => lu
[label] => Lunch
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
)
)
[3] => Array
(
[meal_type] => su
[label] => Supper
[calorie_limit] => 30
[total_calorie] => 0
[data] => Array
(
)
)
[4] => Array
(
[meal_type] => dn
[label] => Dinner
[calorie_limit] => 20
[total_calorie] => 0
[data] => Array
(
[0] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
)
)
the output to be like this
Array
(
[0] => Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)
[1] => Array
(
[0] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
[2] => Array
(
[0] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[1] => Array
(
)
[2] => Array
(
)
[3] => Array
(
)
[4] => Array
(
)
)
)
You need to do it like below:-
$data_array= array_column($array,'data');
$count = 0;
foreach($data_array as $data){
$real_count = count($data);
if($real_count > $count){
$count = $real_count;
}
}
echo $count;
$final_array = [];
foreach($data_array as $data_arr){
for($i=0;$i< $count; $i++){
$final_array[$i][] = (count($data_arr[$i])>0)? $data_arr[$i]: array();
}
}
echo "<pre/>";print_r($final_array);
Output:-https://eval.in/925383
Reference:- PHP: array_column - Manual
You can use array_column() function
Example :-
<?php
$array = [
[
"meal_type" => "bf",
"data" => [
"name" => "test",
"email" => "test#ymail.com"
]
],
[
"meal_type" => "bf",
"data" => []
]
];
echo "<pre>";
print_r(array_column($array, "data"));
?>
You can do this using foreach and array_push.
$data_item_array = [];
foreach ($array as $item) {
$data = $item['data'];
foreach ($data as $t) {
array_push($data_item_array, $t);
}
}
print_r($data_item_array);
Output will be :
Array
(
[0] => Array
(
[id] => 109
[label] => testfrom
[quantity] => 12
[unit] => g
)
[1] => Array
(
[id] => 118
[label] => test
[quantity] => 200
[unit] => oz
)
[2] => Array
(
[id] => 121
[label] => test
[quantity] => 10
[unit] => g
)
[3] => Array
(
[id] => 120
[label] => testfrom
[quantity] => 12
[unit] => g
)
[4] => Array
(
[id] => 119
[label] => test
[quantity] => 200
[unit] => oz
)
)

PHP array reformat to highchart json ob

I am try to format an array to a JSON object that highcharts supports. My array from the database is as follows:
Array
(
[0] => Array
(
[Group_ID] => 1
[Name] => A line graph
[month] => 4
[amount] => 7700
)
[1] => Array
(
[Group_ID] => 2
[Name] => B Line graph
[month] => 4
[amount] => 390
)
[2] => Array
(
[Group_ID] => 1
[Name] => A line graph
[month] => 5
[amount] => 5000
)
[3] => Array
(
[Group_ID] => 2
[Name] => B line graph
[month] => 5
[amount] => 210
)
)
I need to create an array like this to be able to create a highchart compatible JSON object:
Array
(
[0] => Array
(
[name] => A revenue
[data] => Array
(
[4] => 7700 //amount for the fourth month
[5] => 5000 //amount for the fifth month
)
)
[1] => Array
(
[name] => B revenue
[data] => Array
(
[4] => 390 //amount for the fourth month
[5] => 210 //amount for the fifth month
)
)
)
I have managed to come up with this array using my foreach but I cant seem to find a way to do it correctly:
Array
(
[0] => Array
(
[name] => A line graph
[amount] => 7700
[month] => 4
)
[1] => Array
(
[name] => B line graph
[amount] => 390
[month] => 4
)
[2] => Array
(
[name] => A line graph
[amount] => 5000
[month] => 5
)
[3] => Array
(
[name] => B line graph
[amount] => 210
[month] => 5
)
)
My foreach:
foreach ($data as $key => $value) {
$r[] = [
'name' => $value['Line_GraphName'],
'data' => $value['amount'],
'month' => $value['month']
];
}
You can create it very simply like this:
foreach ($data as $value) {
$r[$value['Group_ID']]['name'] = $value['Line_GraphName'];
$r[$value['Group_ID']]['data'][$value['month']] = $value['amount'];
}
Loop and create a result with Group_ID as the key and add the name key and value
Add data array and append month as key and amount as value
If you don't like having Group_ID as the key and want to re-index:
$r = array_values($r);
The solution using array_walk and array_values functions:
$result = [];
array_walk($data, function($v) use(&$result) {
$key = $v['Group_ID'] . $v['Name'][0]; // compound key: "group_id + revenue's prefix"
if (!isset($result[$key])) {
$result[$key] = ['name' => $key[1] . " Revenue", 'data' => [$v['month'] => $v['amount']]];
} else {
$result[$key]['data'][$v['month']] = $v['amount'];
}
});
print_r(array_values($result));
The output:
Array
(
[0] => Array
(
[name] => A Revenue
[data] => Array
(
[4] => 7700
[5] => 5000
)
)
[1] => Array
(
[name] => B Revenue
[data] => Array
(
[4] => 390
[5] => 210
)
)
)

Replace numeric array keys with associative keys from array

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

How do I find max values in a multidimensional array?

I Have an array which looks like this:
Array
(
[0] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-15 13:27:35
[status] => 3
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 6
)
)
[1] => Array
(
[0] => Array
(
[product] => 2003
[date] => 2010-09-12 13:27:35
[status] => 1
)
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 4
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
[2] => Array
(
[0] =>
[1] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
[2] => Array
(
[product] => 2004
[date] => 2010-09-18 13:27:35
[status] => 1
)
)
I want to "collapse" each second dimension array and obtain the max DATE value and the max status value.So the first index would return 2010-09-18 13:27:35 and '6' etc.
The problem is further complicated by the empty array in the last index. I would like to use this empty array and report it as the MAX date and status.
Thank in advance!
Thanks for looking everybody. I figured it out.
$date=array();
$status=array();
$availability=array();
foreach($set as $key => $value)
{
foreach($value as $value2)
{
if(isset($value2[1]))
{
$date[$key][]=$value2[1];
$status[$key][]=$value2[2];
}
else
{
$date[$key][]='2022-09-18 13:27:35';
$status[$key][]='0';
}
}
$availability[$key]=array(max($date[$key]),min($status[$key]));
}

Categories