0

I have array an array with this value

array:11 [
 "id" => 4
  "title" => "CLUSTER AIRING"
  "channel_title" => "CLUSTER AIRING"
  "parent_id" => 2
  "start_date" => "2022-09-30 10:59:00"
  "end_date" => "2022-09-30 13:58:00"
  "image_url" => "https://stag-sections-image.visionplus.id/media/"
  "content_core_id" => 2
  "type" => "Channel"
  "content_type" => "Channel"
  "is_upcoming" => true
]

I already make this code to filter start_date & end_date this filter already works. but when I changed the valueis_upcoming from true to it's not changed. but when I make dd() to $item this is already changed to false. Here is my code

$data = array_values(array_filter($data, function($item){
       if (($item['start_date']) <= now() && ($item['end_date'] >= now()) ) {
               $item['is_upcoming'] = false;
               return $item;
       }
       if ($item['start_date'] > now()) {
               $item['is_upcoming'] = true;
               return $item;
           }
}));
0

1 Answer 1

0

The callback is used to return a boolean value to determine whether to include or exclude the object/value in array_filter. But you're using it to return the entire data which makes the callback always return true. Use array_map() instead of array_filter().

$data = array_map(function($item){
    if (($item['start_date']) <= now() && ($item['end_date'] >= now()) ) {
        $item['is_upcoming'] = false;
        return $item;
    }
    if ($item['start_date'] > now()) {
        $item['is_upcoming'] = true;
        return $item;
    }
}, $data);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.