I have an array like the one below:
$v = array(1,2,3,4,2,3);
How do I get the keys of all elements in array where the value is equal to 2?
I have an array like the one below:
$v = array(1,2,3,4,2,3);
How do I get the keys of all elements in array where the value is equal to 2?
If you have a value in an array and you want to get the keys you can use array_keys()
with the optional search_value
:
$v = array(1,2,3,4,2,3);
$keys = array_keys($v, '2');
print_r($keys);
// Array
// (
// [0] => 1
// [1] => 4
// )
For the output check https://3v4l.org/N8EBH