Reputation: 446
Ok it's late night and I'm working non-stop from too many hours so here's why I don't manage to understand what's the problem here. I have an array:
Array
(
[bob] =>
[mike-2] =>
[tara] => example.com
)
I want to get the key searching for value so I'm using array_search:
// With an if statement...
if(in_array($_SERVER['SERVER_NAME'], $array)!==false)
{
// something
}
// ... and also directly with this
$key = array_search($_SERVER['SERVER_NAME'], $array);
echo $key;
Result? Always false! There's no way for me to get tara when I'm looking for example.com. What the heck am I missing? I even tried replacing $_SERVER['SERVER_NAME'] directly with "example.com" but of course it still doesn't work.
Edit: it was a typo error... damn. I wasted 2 hours for this.
Upvotes: 0
Views: 411
Reputation: 1828
Stop working. This is an actual answer. Just stop. Whenever it comes to you wasting two hours on a typo you're doing nobody any good, especially yourself.
Rest, you're not getting anywhere like this.
Upvotes: 5
Reputation: 111
Array search is case sensitive, $_SERVER['SERVER_NAME'] will return the name in upper case so you have to convert it lower case in your case to work properly, additionally try to map the array also to lowercase Try the given example
$data = array
(
'bob' =>'',
'mike-2' =>'',
'tara' =>'example.com'
);
array_search(strtolower($_SERVER['SERVER_NAME']), array_map('strtolower', $data));
Upvotes: 0
Reputation: 169
Try this instead
$test= array('bob' => '','mike' => '','tara' => 'serverName');
while(list($key,$value) = each($test))
{
if($value==$_SERVER['SERVER_NAME'])
{
echo $key;
break;
}
}
Upvotes: 0