Jump to content

Delete Item from Array


Dysan

Recommended Posts

Well... you could pass something in the url such as example.php?action=add&id=1 or example.php?action=del&id=1... or you could just pass an id and if it exists delete it and if it doesn't add it.  Depends on what its for and what you want it to do... but the code would look something like this...

<?php
  $id = $_GET['id'];
  $action = $_GET['action'];
  switch($action){
  case 'add':
    $myArray[$id] = something;
    break;
  case 'del':
    unset($myArray[$id]);
    break;
  }
?>

Link to comment
Share on other sites

LOL... you should have just reposted... you completley changed your question...

Do you want to search for a value... or an id?

 

I'll assume you are wanting to pass an id and if it is there then delete it, if not create it.

 

<?php
$id = $_GET['id'];
if(isset($myArray[$id])){
  unset($myArray[$id]);
} else {
  $myArray[$id] = something;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.