php - Check for duplicate in array -
i have created html form passes data text file , text file saves data on array.
i cannot figure out how go checking see if value exists before putting input in array or text file.
name: <input type="text" name="name" /><br /> email: <input type="text" name="email" /><br /> <input type="submit" name="save" value="submit" /><br /> <?php $myarray = array(); if (isset($_post['save'])) { /*the file created in same directory php code resides *a+ not overwrite text*/ $myfile = fopen("donorlist.txt", "a+") or die("unable open file!"); //get data entered user form fwrite($myfile, $_post['name']); fwrite($myfile, " "); fwrite($myfile, $_post['email']); fwrite($myfile, "\r\n"); //next line fclose($myfile);//close file textoutput();//call function } print_r($myarray); //creating function make code more readable function textoutput() { $lines = file('donorlist.txt'); foreach ($lines $lines_num => $line) { echo "user input: ".htmlspecialchars($line)."<br />\n"; } $file = fopen("donorlist.txt", "r"); while(!feof($file)) { $myarray[]= fgets($file); } fclose($file); } ?>
you may try after fullfilling array
$myarray = array_unique($myarray);
also can check if value in array before pushing it:
while(!feof($file)) { $line = fgets($file); if (!in_array($line, $myarray)) { $myarray[]= $line; } }
here have info complexity: add array if isn't there already
Comments
Post a Comment