Jump to content


Photo

Filter array by KEY

php array keys filter

Best Answer Barand, 16 May 2018 - 05:47 PM

Rather than hardcoding the date as I did above

$sunday = (new DateTime('last sunday'))->format('Y-m-d');                                            
$arr = array_filter($arr, function($v) use ($sunday) { return $v < $sunday;}, ARRAY_FILTER_USE_KEY);
Go to the full post


  • Please log in to reply
5 replies to this topic

#1 KillGorack

KillGorack
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 16 May 2018 - 05:10 PM

I would like to create an array from the one below that can sort of filter using date ranges;

 

For example, make an array that contains data using date <= 2018-05-09 without a loop.

 

A loop is an option just wondering if you can filter by keys somehow.

Array
(
    [2018-04-23 21:31:40] => -1.174
    [2018-04-24 15:43:59] => -1.015
    [2018-04-26 00:14:10] => -0.37
    [2018-04-30 18:41:51] => -1.042
    [2018-05-01 20:08:40] => -0.72
    [2018-05-02 22:11:52] => -0.107
    [2018-05-07 18:40:12] => -0.298
    [2018-05-09 16:35:38] => -0.36
    [2018-05-10 01:14:27] => 0.408
    [2018-05-14 20:49:54] => 1.549
)


#2 KillGorack

KillGorack
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 16 May 2018 - 05:34 PM

It's a little reckless, but I'm implementing the loop like this.

$d = date('Y-m-d', strtotime('last Sunday', strtotime(date('Y-m-d'))));
$d = $d." 23:59:59";
foreach($combine as $key => $b){
  if(strtotime($key) >= strtotime($d)){
    unset($combine[$key]);
  }
}

A filter that does the same would be awesome..



#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,169 posts

Posted 16 May 2018 - 05:40 PM

Use the ARRAY_FILTER_USE_KEY flag

$arr = array_filter($arr, function($v) { return $v < '2018-05-09';}, ARRAY_FILTER_USE_KEY);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 KillGorack

KillGorack
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 16 May 2018 - 05:43 PM

Walked around array_filter for a while, never seen the ability to put that tag there. thanks!



#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,169 posts

Posted 16 May 2018 - 05:47 PM   Best Answer

Rather than hardcoding the date as I did above

$sunday = (new DateTime('last sunday'))->format('Y-m-d');                                            
$arr = array_filter($arr, function($v) use ($sunday) { return $v < $sunday;}, ARRAY_FILTER_USE_KEY);

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 KillGorack

KillGorack
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 16 May 2018 - 05:51 PM

great also changed the code from the last second on Sunday to the first on Monday.

 

looks like that works.

 

I appreciate the help, thanks.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users