note 130001 added to function.in-array
| From: | mmjunior@nospam.live.com | Date: | Wed, 29 Jan 2025 18:06:34 +0000 |
| Subject: | note 130001 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221819@lists.php.net to get a copy of this message | ||
If you need to provide an array of values ââto be searched for in the values
ââof another array, or even in the keys of another array, this possibility does not exist
natively in PHP, but if we use the str_replace() function cleverly we can achieve this goal.
function any_in_array(array $needle, array $haystack, string $where = 'keys'): bool
{
if ($where == 'keys') {
$invoke = 'array_keys';
} else if ($where == 'values') {
$invoke = 'array_values';
} else {
return false;
}
str_replace($needle, '', implode('¬¬', $invoke($haystack)),
$occurrences);
return $occurrences > 0;
}
var_dump(any_in_array(['abc', 'PHP_SELF', 'def'], $_SERVER,
'keys')); // bool(true)
print_r($_SERVER);
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 191.184.0.28)
----
Manual Page -- https://php.net/manual/en/function.in-array.php
Edit -- https://main.php.net/note/edit/130001
Del: integrated -- https://main.php.net/note/delete/130001/integrated
Del: useless -- https://main.php.net/note/delete/130001/useless
Del: bad code -- https://main.php.net/note/delete/130001/bad+code
Del: spam -- https://main.php.net/note/delete/130001/spam
Del: non-english -- https://main.php.net/note/delete/130001/non-english
Del: in docs -- https://main.php.net/note/delete/130001/in+docs
Del: other reasons-- https://main.php.net/note/delete/130001
Reject -- https://main.php.net/note/reject/130001
Search -- https://main.php.net/manage/user-notes.php