Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter array in php using array_search value less than [duplicate]

Tags:

arrays

php

filter

I have following array in php which I am trying to filter on the date. Date is stored as "Ymd" format in DropDate key.

I would like to apply filter where Dropdate < 20171102 so it only shows me 1 record.

array(3) {
    [0]=>
    array(30) {
        ["Id"]=>
        int(18762)
        ["DropDate"]=>
        string(8) "20171101"
    }
    [1]=>
    array(30) {
        ["Id"]=>
        int(18760)
        ["DropDate"]=>
        string(8) "20171102"
    }
    [2]=>
    array(30) {
        ["Id"]=>
        int(18258)
        ["DropDate"]=>
        string(8) "20171102"
    }
}

I have used the following code but it adds [{0:"Id":18280,"...] when I json_encode it.

$records = array_filter($bookings, function($value) {
        $todays_date = date('Ymd');
        return $value['DropDate'] < (int)$todays_date;
    });

I am not sure how I can do an array_search on less than value for dropdate field.

$todays_date = date('Ymd');
    if(($key = array_search($todays_date, array_column($student,'DropDate'))) !== false) {
        unset($student[$key]);
    }

Then when I do json_encode to return it to my app I get strange behaviour like it adds 0 and 1 infront of each object.

 [{"0":{"Id":18280,"DropDate":"20171030"},"1":{"Id":18284,"DropDate":"20171101"}}]  

My application expects this with no 0 or 1

 [{"Id":18280,"DropDate":"20171030"},{"Id":18284,"DropDate":"20171101"}]
like image 358
snowflakes74 Avatar asked Oct 22 '25 18:10

snowflakes74


2 Answers

array_filter is the right way to go.

Note: Comparing two date strings will work just as well as comparing two timestamps, but IMHO it's better to use timestamps.

Try this:

$records = array_filter($bookings, function ($row) {
    return $row['DropDate'] < date('Ymd');
});

echo json_encode($records);

Output:

[{"Id":18762,"DropDate":"20171101"}]

eval.in demo

like image 56
Ethan Avatar answered Oct 25 '25 10:10

Ethan


I came up with this using the powerful DateTime along with array_filter:

<?php
$bookings = [
  ['Id' => 18762, 'DropDate' => '20171101'],
  ['Id' => 18760, 'DropDate' => '20171102'],
  ['Id' => 18258, 'DropDate' => '20171102'],
];

$records = array_filter($bookings, function ($row) {
  $today = DateTime::createFromFormat('Y-m-d', date('Y-m-d'))->format('U');
  $booking_date = DateTime::createFromFormat('Ymd', $row['DropDate'])->format('U');
  return $booking_date < $today;
});

print_r($records);

This gives:

Array
(
    [0] => Array
        (
            [Id] => 18762
            [DropDate] => 20171101
        )

)

Nicer to work with timestamps. Hope this helps.

Demo: https://eval.in/891570


Regarding the JSON stuff at the end of the question

$input = <<<JSON
[{"0":{"Id":18280,"DropDate":"20171030"},"1":{"Id":18284,"DropDate":"20171101"}}] 
JSON;

You can decode from your source like:

$decoded = json_decode($input, true);
$bookings = array_values($decoded[0]);

And then use the above code.

Demo for this: https://eval.in/891591

like image 30
alistaircol Avatar answered Oct 25 '25 08:10

alistaircol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!