Gp Master Gp Master -4 years ago 53
PHP Question

Php Get keys of all items in array where value is equal to a specific constant

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?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download