I have an array in PHP as the following:
[0] => Array
(
[41] => 20
[2] => 42
[3] => 30
[12] => 94
[32] => -2
[39] => -3
[40] => -15
)
I just want to fetch the index number of a particular key, like the index number of the key 41
is 0
, index number of the key: 2
is 1
, and so on. So please tell me how to do it in PHP. Thanks
Quick way
$number = array_search($index, array_keys($array));
Long way
$i = 0;
$number = false;
foreach ($array as $key => $value){
if ($key == $index){
$number = $i;
break;}
$i++;
}
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