I have to filter this array to see only future items.
How do I unset items from which the timeEnd has expired?
i.e when I call this array at 17:00 there's only array[2] left.
Array
(
[0] => Array
(
[id] => 3034
[date] => 28-09-2016
[timeStart] => 08:30
[timeEnd] => 09:30
[description] => User_A
[locationId] => 1
[roomId] => 8
[relationId] => 104
)
[1] => Array
(
[id] => 2524
[date] => 28-09-2016
[timeStart] => 08:30
[timeEnd] => 12:00
[description] => User_B
[locationId] => 1
[roomId] => 5
[relationId] => 86
)
[2] => Array
(
[id] => 2533
[date] => 28-09-2016
[timeStart] => 09:00
[timeEnd] => 18:00
[description] => User_C
[locationId] => 1
[roomId] => 4
[relationId] => 31
)
)
foreach ($reservations as $key=>$reservation) {
$expireDate = $reservation['date'].' '.$reservation['timeEnd'];
if (strtotime($expireDate) <= strtotime('now')){
unset($reservations[$key]);
}
}
Related
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);
I'm stuck on this problem and I hope someone can help me on that.
I have an array which I want to group by a specific key. The only problem is that I want to have a new array whenever the value of the key changes during the loop. Here is an example of the array.
Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[2] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
[3] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[5] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[6] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
I did
foreach($report_items as $item)
{
$groupedItems[$item['grouped_by']][] = $item;
}
and I got
Array
(
[1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[4] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
)
What I'm actually looking for is something like this, where the arrays are separated during the loop and a suffix is added to the key whenever the value changes.
Array
(
[1.1] => Array
(
[0] => Array
(
[id] => 972
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
[4.1] => Array
(
[0] => Array
(
[id] => 644
[user_id] => 2
[user_field_48] => 4
[project] => 123 — QHV
[duration] => 15:00
[grouped_by] => 4
)
[1] => Array
(
[id] => 631
[user_id] => 2
[user_field_48] => 4
[project] =>
[duration] => -5:00
[grouped_by] => 4
)
)
[1.2] => Array
(
[0] => Array
(
[id] => 630
[user_id] => 2
[user_field_48] => 1
[project] =>
[duration] => 22:00
[grouped_by] => 1
)
[1] => Array
(
[id] => 971
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[2] => Array
(
[id] => 973
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
[3] => Array
(
[id] => 974
[user_id] => 2
[user_field_48] => 1
[project] => 100 — NLO
[duration] => 1:00
[grouped_by] => 1
)
)
)
I'm not really aware of a simple way to solve this, so I would appreciate any help. Thank you!
Not going to bother to recreate your full array here, I am using a reduced version with just the id and the grouped_by value, but the principle is of course exactly the same if you do it with your "full" array.
I am using a counter array to keep track of which "iteration" of a specific group we are currently dealing with. In classic control break implementation logic, I am comparing the grouped_by of the current record, with that of the previous one - if those differ, then the counter for this grouped_by value has to be incremented by 1. And then, I am simply creating the array key to use, by combining the grouped_by value, and the current count for it.
$data = [['id' => 972, 'grouped_by' => 1], ['id' => 664, 'grouped_by' => 4], ['id' => 631, 'grouped_by' => 4], ['id' => 630, 'grouped_by' => 1], ['id' => 971, 'grouped_by' => 1], ['id' => 973, 'grouped_by' => 1], ['id' => 974, 'grouped_by' => 1]];
$grouped_result = $grouped_by_counts = [];
$previous_grouped_by = null; // a value that will never occur in the data
foreach($data as $datum) {
if($datum['grouped_by'] !== $previous_grouped_by) {
$grouped_by_counts[$datum['grouped_by']] =
isset($grouped_by_counts[$datum['grouped_by']]) ?
$grouped_by_counts[$datum['grouped_by']] + 1 : 1;
}
$grouped_result[
$datum['grouped_by'].'.'.$grouped_by_counts[$datum['grouped_by']]
][] = $datum;
$previous_grouped_by = $datum['grouped_by'];
}
print_r($grouped_result);
Live example: https://3v4l.org/odtie
I want to convert below array.
Criteria is,
If projectId same then store same project id data under array of project which contains projectId as key.
Array
(
[0] => Array
(
[PMST] => Array
(
[id] => 4
[project_id] => 25
[task_name] => Final task 3
[start_date] => 2016-06-21 00:00:00
[end_date] => 2016-06-29 00:00:00
)
[PMSP] => Array
(
[id] => 25
[project_name] => Project 3
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
)
[1] => Array
(
[PMST] => Array
(
[id] => 9
[project_id] => 28
[task_name] => Task Test 333 edit
[start_date] => 2016-06-19 00:00:00
[end_date] => 2016-06-29 00:00:00
)
[PMSP] => Array
(
[id] => 28
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
)
[2] => Array
(
[PMST] => Array
(
[id] => 1
[project_id] => 28
[task_name] => Task 1
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
[PMSP] => Array
(
[id] => 28
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
)
)
Desire Output
Array
(
[25] => Array
(
[PMSP] => Array
(
[id] => 25
[company_id] => 1114701
[project_name] => Project 3
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
[taskdetails] => Array
(
[0] => Array(
[PMST] => Array
(
[id] => 4
[project_id] => 25
[company_id] => 1114701
[task_name] => Final task 3
[start_date] => 2016-06-21 00:00:00
[end_date] => 2016-06-29 00:00:00
)
)
)
)
[28] => Array
(
[PMSP] => Array
(
[id] => 28
[company_id] => 1114701
[project_name] => Project Employee Test
[start_date] => 2016-06-10 00:00:00
[end_date] => 2016-06-30 00:00:00
)
[taskdetails] => Array
(
[0] => Array
(
[PMST] => Array
(
[id] => 9
[project_id] => 28
[company_id] => 1114701
[task_name] => Task Test 333 edit
[start_date] => 2016-06-19 00:00:00
[end_date] => 2016-06-29 00:00:00
)
)
[1] => Array(
[PMST] => Array
(
[id] => 1
[project_id] => 28
[company_id] => 1114701
[task_name] => Task 1
[start_date] => 2016-06-01 00:00:00
[end_date] => 2016-06-04 00:00:00
)
)
)
)
)
Loop over your input array using foreach, adding the PMST data to an output array as you go. It looks like it's safe to assume the PMSP data for two tasks on the same project will be the same.
$output = [];
foreach ($input as $task) {
if (!isset($output[$task["PMSP"]["id"]])) {
$output[$task["PMSP"]["id"]] = ["PMSP" => $task["PMSP"], "taskdetails" => []];
}
$output[$task["PMSP"]["id"]]["taskdetails"][] = $task["PMST"];
}
See the below example
Example
$data = [];
foreach ($a as $b) {
$key = $b["PMSP"]["id"];
if (!isset($data[$key])) {
$data[$key] = ["PMSP" => $b["PMSP"], "taskdetails" => []];
}
$data[$key]["taskdetails"][] = $b["PMST"];
}
echo "<pre>";
print_r($data);exit();
Your latest data
Example 2
I have an array but I want to generate other array from that one..
Array
(
[0] => Array
(
[supplier] => Billy
[total_bookings] => 5
[year] => 2016
[month] => 6
[user_id] => 4
[sales_revenue] => 1180
[net_revenue] => 1180
)
[1] => Array
(
[supplier] => XYZ1
[total_bookings] => 3
[year] => 2016
[month] => 6
[user_id] => 2
[sales_revenue] => 642
[net_revenue] => 642
)
[2] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2016
[month] => 3
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
[3] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2015
[month] => 10
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
to new array :
Array
(
[2016] => Array(
[6] => Array
(
[0] => Array(
[supplier] => Billy
[total_bookings] => 5
[user_id] => 4
[sales_revenue] => 1180
[net_revenue] => 1180
)
[1] => Array
(
[supplier] => XYZ1
[total_bookings] => 3
[user_id] => 2
[sales_revenue] => 642
[net_revenue] => 642
)
)
[3] => Array
(
[0] => Array
(
[supplier] => Billy
[total_bookings] => 1
[year] => 2016
[month] => 3
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
)
[2015] => Array(
[10] => Array
(
[supplier] => Billy
[total_bookings] => 1
[user_id] => 4
[sales_revenue] => 30
[net_revenue] => 30
)
)
)
The solution using array_fill_keys, array_column(available since PHP 5.5), array_walk and array_diff_key functions:
// supposing $arr is your initial array
$years = array_fill_keys(array_column($arr, 'year'), []);
array_walk($arr, function($v) use(&$years){
if (key_exists($v['year'], $years)) {
$years[$v['year']][$v['month']][] = array_diff_key($v, ['year'=>0, 'month'=>0]);
}
});
print_r($years);
Try this one and let me know:
$new_arr = array();
foreach($arr as $val){
$temp = array('supplier' => $val['supplier'], 'total_bookings' => $val['total_bookings'], 'user_id' => $val['user_id'], 'sales_revenue' => $val['sales_revenue'], 'net_revenue' => $val['net_revenue']);
array_push($new_arr[$val['year']][$val['month']], $temp);
}
print_r($new_arr);
Try this one:-
$res = [];
foreach($array as $record){
$year = $record['year'];
$month = $record['month'];
unset($record['year'],$record['month']);
$res[$year][$month][] = $record;
}
echo '<pre>'; print_r($res);
I've following array:
Array
(
[1] => Array
(
[team1_id] => 2
[agegroup_id] => 18
[team2_id] => 3
[team_ground] => Adeilade
[matchdate] => 2016-04-01
[matchtime] => 9:00 AM
)
[2] => Array
(
[team1_id] => 3
[agegroup_id] => 18
[team2_id] => 2
[team_ground] => Adeilade
[matchdate] => 2016-04-13
[matchtime] => 10:00 AM
)
[3] => Array
(
[team1_id] => 2
[agegroup_id] => 18
[team2_id] => 3
[team_ground] => Adeilade
[matchdate] => 2016-04-20
[matchtime] => 2:00 PM
)
[4] => Array
(
[team1_id] => 3
[agegroup_id] => 18
[team2_id] => 2
[team_ground] => Adeilade
[matchdate] => 04/07/2016
[matchtime] => 5:00 PM
)
[6] => Array
(
[team1_id] => 9
[agegroup_id] => 36
[team2_id] => 4
[team_ground] => Motera Stadium
[matchdate] => 2016-04-13
[matchtime] => 9:00 AM
)
[7] => Array
(
[team1_id] => 4
[agegroup_id] => 36
[team2_id] => 9
[team_ground] => Motera Stadium
[matchdate] => 2016-04-13
[matchtime] => 5:00 PM
)
[9] => Array
(
[team1_id] => 1
[agegroup_id] => 37
[team2_id] => 8
[team_ground] => Eden Garden
[matchdate] => 2016-04-18
[matchtime] => 7:00 PM
)
[10] => Array
(
[team1_id] => 8
[agegroup_id] => 37
[team2_id] => 1
[team_ground] => Eden Garden
[matchdate] => 2016-04-25
[matchtime] => 8:00 PM
)
[11] => Array
(
[team1_id] => 1
[agegroup_id] => 37
[team2_id] => 8
[team_ground] => Eden Garden
[matchdate] => 04/26/2016
[matchtime] => 8:00 PM
)
[0] => Array
(
[agegroup_id] => 18
)
[5] => Array
(
[agegroup_id] => 36
)
[8] => Array
(
[agegroup_id] => 37
)
)
If array is having only one element then I've to unset it. In this case I want to unset array index 0,5,8 in PHP. How to unset the array index when it is having less elements?
You can simply use array_filter over here as
$result = array_filter($your_array,function($v){ return count($v) > 1;});
print_r($result);
You can also use a simple foreach:
foreach ($array as $key => $value)
{
if(sizeOf($array[$key]) < 2)
unset($array[$key]);
}
Here is a working DEMO