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
https://forums.phpfreaks.com/topic/97729-deleting-an-element-in-an-array/
Share on other sites

Well, assuming you are storing data as:

 

Person Name,[email protected]

 

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

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

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

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.