0

Hey guys im trying to search an array for an value and then remove it. But im searching with a php variable and that seems to be the problem. So this is what I have so far:

if(isset($_REQUEST['Admin'])){
    $arr = array('HAUGMA1', 'sdasd', 'dasdasda', 'sadasd');
    $key=array_search($_REQUEST['Admin'],$arr);
    if($key!==false) 
        unset($arr[$key]);
    print_r($arr);
}

The $_REQUEST retrieves the value HAUGMA1 but when I print out the array it didn't remove HAUGMA1 from it. What am I doing wrong? When I'm using this:

$key=array_search('HAUGMA1',$arr); 

It is working.

4
  • what is the actual value of $_REQUEST['Admin'] when you echo it? Commented Nov 15, 2012 at 13:47
  • you have used second if wrong. it should be if($key!=FALSE) Commented Nov 15, 2012 at 13:47
  • 1
    @AbhishekBhatia not according to the manual... php.net/manual/en/function.array-search.php Commented Nov 15, 2012 at 13:48
  • @thescientist the value is HAUGMA1 Commented Nov 15, 2012 at 13:49

4 Answers 4

1

I think the problem is in relation to the value that is coming from the $_REQUEST variable.
I made the following test to show two questions:

//Array definition
$arr = array('HAUGMA1', 'sdasd', 'dasdasda', 'sadasd');
//Search
$admin = 'dasdasda';

/* First sample */

$time = microtime();

$arr = array_filter($arr, function($key) use($admin) {
    return $key != $admin;
});

print_r($arr);
echo '<br>Execution time: '.(microtime()-$time).'<br>';

/* Second sample */

$time = microtime();

$key = array_search($admin, $arr);
if($key !== false)
    unset($arr[$key]);

print_r($arr);
echo '<br>Execution time: '.(microtime()-$time).'<br>';

First: using the array_search has approximately 4x faster than array_filter.

Second: the way that your script is, the item is removed from the array without problem. Try to debug the value that is coming from the variable $_REQUEST

Sign up to request clarification or add additional context in comments.

2 Comments

an var_dump of $_REQUEST['Admin'] gives me string(9) "HAUGMA1 "
That's the problem. Use the "trim" to remove extra spaces in $_REQUEST variable: array_search(trim($_REQUEST['Admin']), $arr);
0

try this (closure PHP >= 5.3)

$admin = $_REQUEST['Admin'];

$arr = array_filter($arr, function($key) use($admin) {
    return $key != $admin;
});

Comments

0

When i set manually $_REQUEST['Admin'] to "HAUGMA1", it's working. Are you sure there is no whitespace in the beginning or the end of your string ?

Try with this :

$key = array_search(trim($_REQUEST['Admin']), $arr);

1 Comment

In Wayne Whitty's response he used strtoupper and trim and the OP said that it still wasn't working.
0

array_search is case sensitive. Use strtoupper and trim in order to conform the incoming variable to the values in your array. trim() will get rid of trailing white space and other unwanted characters.

if(isset($_REQUEST['Admin'])){
    $arr = array('HAUGMA1', 'SDASD', 'ETC');
    $key = array_search(strtoupper(trim($_REQUEST['Admin'])), $arr);
    if($key !== false){ 
        unset($arr[$key]);
    }
    print_r($arr);
 }

3 Comments

nope not wokring result is:Array ( [0] => HAUGMA1 [1] => SDASD [2] => ETC )
Can you var_dump($_REQUEST['Admin']) and tell us the result?
The result is: string(9) "HAUGMA1 "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.