For example if I am searching for a key with a value 5
in my array $cake
, I could use the following code:
$cake = array("a"=>6,"b"=>5,"c"=>6);
echo array_search(5, $cake, true); // returns "b";
But if my $cake
array contains multiple matches, only the first match is returned:
$cake = array("a"=>6,"b"=>5,"c"=>5,"d"=>5,"e"=>5);
echo array_search(5, $cake, true); // returns "b";
How can I return multiple matches as an array? Like this:
$cake = array("a"=>6,"b"=>5,"c"=>5,"d"=>5,"e"=>5);
// return array("b","c","d","e");
VLOOKUP with Multiple Results To lookup multiple matches with the VLOOKUP Function you need to create a helper column within the table of data. The helper column uses the COUNTIF Function to create a unique ID for each instance. The helper column must be the leftmost column within the data set.
An immediate solution that comes to mind is using the Excel VLOOKUP function, but the problem is that it can only return a single match. Vlookup for multiple values can be done via a combined use of several functions.
As noted in the docs:
If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.
print_r(array_keys($cake, 5, true));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With