为什么array_search失败

$arr =array('username','admin');
foreach($_GET as $k=>$v)
    if(array_search($k, $arr))
        $results[$k] = $v;

print_r($results); // prints nothing and I get a
//$results is undefined error

$_GET contains:

(
     [r] => p/p
     [i] => 9
     [_s] => true
     [r] => 10
     [p] => 1
     [s] => username
     [o] => asc
     [username] => bd
)

so I would expect my $results array to contain 'bd' but instead it is undefined.

array_search returns the key. For username that key is 0, which evaluates to false. You need to check for if (array_search($k, $arr) !== false). You should also initialize $results before the loop, in case none of the keys are present, otherwise $results is never defined.

A much shorter way to do the same thing would be:

$results = array_intersect_key($_GET, array_flip(array('username','admin')));

There is no definition of $results in your code.

I think what you want is this

$arr =array('username','admin');
$results = array();
foreach($_GET as $k=>$v){
    $key = array_search($k, $arr);
    if($key)
        $results[$k] = $v;
}

print_r($results); 

The one liner in deceze answer is a pretty cool solution, but in general for this sort of application you'll want to use in_array(), not array_search(). Think array_search if you want to know where to find it, in_array if you just want to know it's there.