Connecting Tech Pros Worldwide Help | Site Map

deleting line from an array

Member
 
Join Date: Jan 2008
Location: SA
Posts: 44
#1: Aug 5 '08
hi guys

i have a problem here it seems like i dont get a solution.i manage to write to the text file but i want to update the existing line without writting a new line.
my problem is when i want to append at the end of a line my programm write at the beginning of a next line.

example

1 (2008_22)-simon; thuli ; tumi
2 (2008_23)-dude ; dan ;

3 dan;tumu(2008_24) -danken ;bobo;

my problem is i want to add : dan and tumi at the end of the second line
not at the begining of the third line.

someone look at my code and help:

Expand|Select|Wrap|Line Numbers
  1.         function write($lineNumbers,$lineNumber)
  2.     {
  3.         if($_POST["cite1"] && $_POST["cite2"] && $_POST["cite3"])
  4.         $data =$_POST["cite1"].";" .$_POST["cite2"].";".$_POST["cite3"];    
  5.  
  6.         $q=$_POST["path"];
  7.  
  8.         $myArray = file($q."Registry.txt");
  9.         //echo "the linenumber is ".$lineNumbers." the line is ". $myArray[$lineNumbers-1]."<br/>";
  10.  
  11.         //Open registry file and wipe contents
  12.  
  13.         $fp = fopen($q."Registry.txt", "w");
  14.         //$myArray[$lineNumbers-1] = $myArray[$lineNumbers-1].$data;
  15.         //echo $myArray[$lineNumbers-1]."<br />";
  16.         $newdata = $myArray[$lineNumbers-1].$data;
  17.         //echo $newdata."<br/>";
  18.  
  19.         // remove the line from a file
  20.         unset($myArray["$lineNumbers-1"]);
  21.         // put the data at the end of my file
  22.         array_push("$myArray","$newdata");
  23.  
  24.         usort($myArray,"strnatcmp");
  25.  
  26.         //Loop through the registry array
  27.  
  28.         foreach ($myArray as $value)
  29.  
  30.         {
  31.  
  32.             //Write the sorted contents to registry line-by-line
  33.  
  34.               fwrite($fp,$value);
  35.  
  36.  
  37.  
  38.     }
  39.  
  40.  
  41.  
  42.         // Close the registry file
  43.  
  44.         fclose($fp);
  45.         echo "the registry updated successfully <br />";
  46.         $myArray1 = file($q."Registry.txt");
  47.         echo "new registry is ".$myArray1[$lineNumbers-1]."<br />";
  48.         echo "line after is ".$myArray1[$lineNumbers]."<br />";                
  49.  
  50.     }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. ?>
Ajm113's Avatar
Familiar Sight
 
Join Date: Jun 2007
Posts: 161
#2: Aug 5 '08

re: deleting line from an array


PHP should be like C++, where you can make a while loop then get something like fgets into the while loop and count for each line into a int and get the lines into a string array and if you want to replace a certain line just enter,

array[$iLine--] = "My data goes here."; = Line1, so just minus the id number by one.

For removel just make the line equal nothing in double quotes and make a if statment weather if that line has something or not to break the line or not or even enter the text.

Then get all the arrays representing the line and print them back out into the file like new into each line using that if statment.

I wanted to be a bit of a help, but that should give you a idea what to do.

BTW use the PHP tags please! So its easier to read your code. Its that button with the php page uptop in the editor.
Reply