In this case the maximum user assigned key is 4 i.e. purple, so it will be 5 for blue eventually... and the code what you are using exactly returns the right output which is 5
.
If you do print_r()
of your array you will get the idea.. See here
Array
(
[1] => orange
[2] => yellow
[3] => green
[4] => purple
[5] => blue
)
EDIT :
i need to find the key as 4 when i search for purple or blue ! how to
do this
You cannot have a same key in an array or for two different values !
Instead modify your array like this...
<?php
$array = array(1 => 'orange', 2 => 'yellow', 3 => 'green', 4 => 'purple,blue'); //Adding purple and blue seperated by comma...
foreach($array as $k=>$v)
{
if(strpos($v,'purple')!==false)
{
echo $k;// "prints" 4 if you pass purple or blue !
break;
}
}