note 115820 deleted from function.in-array by sobak
| From: | sobak@php.net | Date: | Sat, 25 Feb 2017 06:04:26 +0000 |
| Subject: | note 115820 deleted from function.in-array by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208840@lists.php.net to get a copy of this message | ||
Note Submitter: Chaya Cooper
----
To identify all instances of $needle when both $needle and $haystack are arrays, combine in_array()
with a loop through the array of needles.
I've posted a functional example of it here: http://sandbox.onlinephpfunctions.com/code/930385800c30595129cefb87cfa7eec9d69dce08
$needles = array('John', 'Alex');
$haystack = array('John','Alexander','Kim', 'Michael');
foreach ($needles as $needle) {
// Displays var name & value currently searching for)
echo '$needle: '.$needle;
if (in_array($needle, $haystack) ) {
echo ', True; ';
}
else {
echo ', False; ';
}
}
Result is:
$needle: John, True; $needle: Alex, False;