0

I would like to unset an HTTP Posted key from the array after it echoes the "1" result, but the unset doesn't seem to work. How can I fix this?

$keylist = array('HHH', 'GGG');

if (in_array($_POST["keys"], $keylist)){ 
    echo "1"; 
    unset($keylist[$_POST["keys"]]);
} else {
    echo "0" ;
}

Appreciate any help,

Hobbyist

3
  • Which array do you want the removal to come from? $_POST['keys'] or $keylist? Commented Sep 18, 2017 at 21:41
  • What do you desire your code to do that it doesn't do at the moment? Commented Sep 19, 2017 at 0:03
  • does $_POST['keys'] has array or key name Commented Sep 19, 2017 at 19:16

4 Answers 4

1

Your unsetting $keylist not $_POST

unset($_POST["keys"]);
Sign up to request clarification or add additional context in comments.

Comments

1

You're using the wrong array key for unsetting (You're trying to unset $keylist["HHH"], not, say, $keylist[0]) - you'll need to retrieve the key from the array, and then unset that specifically in order to remove it from the keylist.

$index = array_search($_POST["keys"], $keylist);
if($index!==false) { //YES, NOT DOUBLE EQUALS
    unset($keylist[$index));
}

If $_POST["keys"] is an array of keys, you'll need to use array_keys with a search_value instead.

Array_search documentation: http://php.net/manual/en/function.array-search.php

Array_keys documentation: http://php.net/manual/en/function.array-keys.php

EDIT: Adding a full, working example.

<?
$_POST["keys"]="asdjkgldshglsjhgsdlhgsdlghsdlghsdlgh";
$keylist=array("asdjkgldshglsjhgsdlhgsdlghsdlghsdlgh","derp2");
if(in_array($_POST["keys"], $keylist)) {
    $indexToRemove = array_search($_POST["keys"], $keylist);
    echo "1";
    print_r($keylist);
    unset($keylist[$indexToRemove]);
    print_r($keylist);
} else {
    echo "0";
    print_r($keylist);
}
?>

Another example, this time checking the index itself to see if it is not false:

<?
$_POST["keys"]="asdjkgldshglsjhgsdlhgsdlghsdlghsdlgh";
$keylist=array("asdjkgldshglsjhgsdlhgsdlghsdlghsdlgh","derp2");
$indexToRemove = array_search($_POST["keys"], $keylist);
if($indexToRemove!==false) {
    echo "1";
    print_r($keylist);
    unset($keylist[$indexToRemove]);
    print_r($keylist);
} else {
    echo "0";
    print_r($keylist);
}
?>

Output:

1Array ( [0] => asdjkgldshglsjhgsdlhgsdlghsdlghsdlgh [1] => derp2 ) Array ( [1] => derp2 )

I realize now that I only had one = on the $index!==false check - the reason you need two is because $index is 0 if you're removing the first element of an array. Per PHP documentation,

Warning
This function may return Boolean FALSE, but may also return a non-Boolean
value which evaluates to FALSE. Please read the section on Booleans for more 
information. Use the === operator for testing the return value of this function.

7 Comments

I've tried to search to get the key number in the array and then unset that but it still won't remove the value from the array
Is your $_POST["keys"] variable an array, or a string? If it's an array, array_search won't behave correctly. If it's a string, it should be unsetting, and I'd like to see your code.
<?php $keylist = array('CzcQTFrXTW3HGPCuS8Ss5DUb', 'gtB9DXxvj7QnvMDAQHuB8H7L'); $index = array_search($_POST["keys"], $keylist); if (in_array($_POST["keys"], $keylist)) { echo "1"; if($index!=false) { unset($keylist[$index])); } } else { echo "0" ; } ?> That's the full code, appreciate the help
Testing a few things, I'll update my answer with working code given this information. EDIT: What is $_POST["keys"]? An array of keys, or a single key? What's setting $_POST["keys"], and what is it being set to?
$_POST["keys"] is a user entering a key, for example the user will enter 'CzcQTFrXTW3HGPCuS8Ss5DUb', the script will then echo a 1 and remove what they entered off the list, if they enter anything but a set array value it will echo 0
|
0

in_array($_POST["keys"], $keylist) checks if the posted value $_POST["keys"] is present as a value in $keylist, but unset($keylist[$_POST["keys"]]) uses $_POST["keys"] as the key in $keylist to unset something.

Since in_array() returns true, the posted value is present in the array $keylist. But you can't do unset($keylist['GGG']) because that value is not set.

A simple solution is to use the same values as keys too:

$keylist = array('HHH', 'GGG');
$keylist = array_combine($keylist, $keylist);

More about array_combine().

1 Comment

I used the command $keylist = array_combine($keylist, $keylist); under the array of values, I then added unset($keylist[$_POST["keys"]]) to the true condition of the if (under echo "1";) yet still doesn't unset the value?
0

Your array have keys. So "HHH" in keylist is $keylist[0] and not $keylist['HHH']. You can not unset it because you have to define the array key instead of the array value. Try to search for the array value like this:

array_search($_POST["keys"], $keylist)

This will return the array key for you or false if its not found. Your full code should like look like:

echo "1"; 
$arrkey = array_search($_POST["keys"], $keylist);
if( $arrkey !== false ){
unset($keylist[$arrkey]);
}

Here the PHP Manual for the array_search function: http://php.net/manual/en/function.array-search.php

2 Comments

Thanks Mike, I tried the code you write above in the true condition of the if statement but it still doesn't unset the value from the array
i have posted the part of your code. I tested it succesfull. Be carefull. dont use if( $arrkey == true ) or if( $arrkey ). Use if( $arrkey !== false ) This part is important

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.