Lets say I have an array with these elements:
[
'whatever',
'something else',
'foobar'
]
I know I can iterate over the array and check for foobar using a regex for example. The question is, is there a shorter way of doing that?
Thanks?
<?php
$items = [
'whatever',
'something else',
'foobar'
];
$matches = preg_grep ('/foobar/', $items);
var_dump($matches);
?>
You don't need to iterate over the array, you can use preg_grep and pass your array instead.
Happy coding
Not regex , but still worth a mention
if(in_array($string,$array)) {
//is shortest i can think of
}
OR
$your_var = in_array($string,$array) ? 'this is foo' : 'something else';