return key of array if value is present PHP -


what fastest way check if variable present in array value , return key of it?

example:

$myarray = [ "test" => 1, "test2" = 2, "test3" = 3];  $var = [1,6,7,8,9]; 

what need if($var in $myarray) return (in case) "test".

is possible without doing 2 loops? there functions in_array() returns key of value if found?

you can use array_search

foreach($var $value) {     $key = array_search($value, $myarray);     if($key === false)     {         //not found     }     else     {         //$key contains index want     } } 

be careful, if value not found function return false may return value can treated same way false, 0 on 0 based array it's best use === operator shown in example above

check documentation more


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -