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;
  }
?>

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;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.