Jump to content

Deleting an element in an array


jdm95lude

Recommended Posts

I'm doing an assignment. Its a guest book you add you name & email and it saves it to a .txt file you view the guest book the txt file writes to the screen there is a text box that the user can enter in a number that is displayed next to the list of guests and it will delete that name from the txt file. I can't figure it out. Here is a printout of the Assignment

http://www.occc.edu/aphilipp/htm_ssp/SSP06_Assignment_Arrays.htm

thanks

Link to comment
Share on other sites

Well, assuming you are storing data as:

 

Person Name,email@address.com

 

... and then pulling it into an array - the very first item in the list is 0

 

So, if you want to remove 0.. simple rewrite the data to visitors.txt without entry 0

 

ex;

delete_visitor.php?id=5
<?php

$visitors_array = file('visitors.txt');

$i = 0;
while($i < count($visitors_array)
{
   if($entry_id != $_GET['visitor_id']) 
   {
      /// rewrite the file without that 'id' (array key) - in this example, remove key #5
   }
   ++$i;
}

 

Concept is there. Work it out. Not that hard.

Link to comment
Share on other sites

Too complicated, you gotta think more efficiently and look for the right functions. This function will remove a value from array by given key and then reassign numeric keys.

 

<?php
function array_remove($array, $key){
   unset($array[$key]);
   return array_values($array);
}
?>

Link to comment
Share on other sites

Too complicated, you gotta think more efficiently and look for the right functions. This function will remove a value from array by given key and then reassign numeric keys.

 

<?php
function array_remove($array, $key){
   unset($array[$key]);
   return array_values($array);
}
?>

 

Thank you

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.