Reputation: 8385
How can I remove a whole array object if a $key=>value
matches?
For example in my data I have ["endDate"]=> string(10) "2017-06-24"
and if that date matches todays date('Y-m-d')
I would like the whole object bock to be removed from the $row
Code:
foreach ($json->data as $row)
{
$date = date('Y-m-d');
if($row->endDate == $date){
$search = array_search($date, array_column('endDate', $row));
unset($row[$search]);
if (!in_array($row->guestEmail, $emails) && date('Y-m-d', strtotime($row->startDate))== date('Y-m-d'))
{
$guests[] = array(
'FirstName' => $row->guestFirstName,
'LastName' => $row->guestLastName,
'email' => $row->guestEmail,
'country' => $row->guestCountry,
'check-in_date' => $row->startDate,
'check-out_date' => $row->endDate,
);
$emails[] = $row->guestEmail;
}
}
}
JSON:
$response = $o->curl(sprintf($url, $propertyID, $pageSize, $pageNumber, $resultsFrom));
$json = json_decode($response);
Upvotes: 1
Views: 53
Reputation: 2337
$date = date('Y-m-d');
foreach ($json->data as $index=> $row){
if($row->endDate == $date) unset($json->data{$index});
}
foreach ($json->data as $row){
if (!in_array($row->guestEmail, $emails) && date('Y-m-d', strtotime($row->startDate))== date('Y-m-d'))
{
$guests[] = array(
'FirstName' => $row->guestFirstName,
'LastName' => $row->guestLastName,
'email' => $row->guestEmail,
'country' => $row->guestCountry,
'check-in_date' => $row->startDate,
'check-out_date' => $row->endDate,
);
$emails[] = $row->guestEmail;
}
}
You may also consider just skipping to the next iteration, since it looks like you are building a guest email list and are not trying to modify the source of the json feed.
$date = date('Y-m-d');
foreach ($json->data as $row){
if($row->endDate == $date) {
continue;
}
if (!in_array($row->guestEmail, $emails) && date('Y-m-d', strtotime($row->startDate))== date('Y-m-d'))
{
$guests[] = array(
'FirstName' => $row->guestFirstName,
'LastName' => $row->guestLastName,
'email' => $row->guestEmail,
'country' => $row->guestCountry,
'check-in_date' => $row->startDate,
'check-out_date' => $row->endDate,
);
$emails[] = $row->guestEmail;
}
}
Between the two options I pose, the second one requires less code and will yield slightly faster execution times.
Upvotes: 2
Reputation: 467
If you place an ampersand before $row in your foreach you can change $json->data directly. From what you wrote, I think I understand your question and this may be what you need.
foreach ($json->data as &$row)
{
$date = date('Y-m-d');
if ($row->endDate == $date) {
$search = array_search($date, array_column('endDate', get_object_vars($row)));
unset($row[$search]);
if (!in_array($row->guestEmail, $emails) && date('Y-m-d', strtotime($row->startDate))== date('Y-m-d'))
{
$guests[] = array(
'FirstName' => $row->guestFirstName,
'LastName' => $row->guestLastName,
'email' => $row->guestEmail,
'country' => $row->guestCountry,
'check-in_date' => $row->startDate,
'check-out_date' => $row->endDate,
);
$emails[] = $row->guestEmail;
}
}
}
If not then rephrase your question so I might provide the answer you need.
Upvotes: 2