1

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);
4
  • Where does the $json object populate the data from? unset($row[$search]) won't work because basically $row is just a copy of the data. You would need an unset method from the class that the $json object is created from. Commented Jun 24, 2017 at 3:10
  • @AuntJamaima I have updated my question to include the JSON creation point :) Commented Jun 24, 2017 at 3:15
  • You want to remove all rows where the date matches today from the json object, correct? Commented Jun 24, 2017 at 3:23
  • @AuntJamaima Yes :) where the $row->endDate = Today Commented Jun 24, 2017 at 3:28

2 Answers 2

2
$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.

Sign up to request clarification or add additional context in comments.

1 Comment

this is also help fulll for me.
2

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.

3 Comments

Thanks however this gives me an error of Warning: array_column() expects parameter 1 to be array, object given in Warning: array_search() expects parameter 2 to be array, null given in
In that line of your code you need to convert your object to an array. I have corrected that line above for you.
It appears that in that line you are searching all end dates in $json->data, correct?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.