0

I'm currently developing a simple IP ban system that bans an IP if something is done twice. I have searched but it seems that the answers I find are ones that use foreach. What I am wondering, is how to remove a value from an array to tidy it up?

code:

function ipCheck($ip) {
$ip1 = array();
$ip2 = array();
if(in_array($ip, $ip2, true)){
    die();
}
elseif(in_array($ip, $ip1){
    array_push($ip2, $ip);
else{
    array_push($ip1, $ip);
  }
 }
}
user1432856
  • 373
  • 1
  • 4
  • 6
  • May be this link can help you: http://stackoverflow.com/questions/369602/how-to-delete-an-element-from-an-array-in-php – Rohit Choudhary Jun 11 '12 at 06:14
  • Is there a way to find out the location of the element if it is found in the array?(the number like $a[1]) – user1432856 Jun 11 '12 at 06:15
  • Use `$index = array_search($ip, $ip_array);`. [`array_splice`](http://www.php.net/manual/en/function.array-splice.php) will return `false` (use `===` or `!==` to check) if it's not in the array, otherwise it will have the 0-based index of the element. – Cat Jun 11 '12 at 06:16
  • You got your anser from @soxxeh :) – Rohit Choudhary Jun 11 '12 at 06:17

1 Answers1

0

How about this?

if(isset($array[$ip]) {
    unset($array[$ip]);
}
Jonathon Hill
  • 3,445
  • 1
  • 33
  • 31