I'm trying to get the key from an array by searching for its value. In the following code, what I don't understand is why array_search()
can't find the key when the $url
file extension is "xls"
but it can when it is "php"
. I've noticed a similar issue of not being able to "find" "xls"
using in_array()
and array_keys()
.
$url='http://mysite.com/hello.xls';
$url='http://mysite.com/hello.php';
$extension_arr=pathinfo($url);
$extension=strtolower($extension_arr['extension']);
$arr=array(
'excel_file'=>'xls',
'excel_file'=>'xlsx',
'php_file' =>'php'
);
$array_search_key=array_search($extension, $arr);
if($array_search_key !== false){
echo $array_search_key;
}
else echo 'crap';
isset()
to perform the function you require when the keys and values are swapped. – DaveRandom Jul 7 at 0:46'excel_file xls'
and'excel_file xlsx'
. Would you mind making an answer flipping the keys and values? – tim peterson Jul 7 at 0:49