Ramalingam Perumal
Ramalingam Perumal

Reputation: 1427

PHP search a string from an array for match using preg_filter

I am search the year 2018 from $data array. It will return the value Jan.

I need to display Jan 2018

Sample Code

$data = Array(
    '2017-08' => 'Aug 2017',
    '2017-09' => 'Sep 2017',
    '2017-10' => 'Oct 2017',
    '2017-11' => 'Nov 2017',
    '2017-12' => 'Dec 2017',
    '2018-01' => 'Jan 2018'
);
$input = preg_quote('2018', '~');
$result = preg_filter('~' . $input . '~', false, $data);
print_r($result);

Auctual Output

Array
(
    [2018-01] => Jan 
)

Expected Output

Array
(
    [2018-01] => Jan 2018
)

Please Advise!

Upvotes: 0

Views: 68

Answers (1)

Stefan M
Stefan M

Reputation: 889

preg_filter works as a search and replace function, so you're simply replacing the found "2018" with nothing. Maybe you were more looking for something like preg-grep?

Upvotes: 2

Related Questions