0

If there is a file student.txt containing students record as following(first, last name, student ID) like:

   John Smith 2320
   Mary McHugh 4572
   Sara Britny 2322

I wanna check to see if the student ID is unique. if there are duplicated IDs display aan error message with the dupicated ID.

hakre
  • 193,403
  • 52
  • 435
  • 836
wael
  • 49
  • 1
  • 2
  • 6

5 Answers5

1
arrayWithId = array
FOR EACH record AS rec IN file
   IF arrayWithId NOT CONTAINS rec THEN
      ADD rec TO arrayWithId
   ELSE
      display error
END FOR
# if you get here without any errors displayed there are no duplicates
Marcus
  • 12,296
  • 5
  • 48
  • 66
0

Just read the file line by line and save all ids. If one id is already saved throw an error.

$line = /* ... */
$data = explode( ", ", $line );

// should contain the id
$id = $data[2];
Tobias
  • 9,170
  • 3
  • 24
  • 30
  • i used: $fh = fopen("students.txt", 'r'); while (!feof($fh)) { $Data = fgets($fh); list($array["first"], $array["last"],$array["id"]) = explode(" ",$Data); $a = ($array["first"], $array["last"],$array["id"]); $newarray[] = $a; },,,, to read and store the data in array – wael Oct 10 '11 at 11:07
  • I created this code to read but how to save, and if one ID is already saved throw error? – wael Oct 10 '11 at 11:14
  • Just save the id in an array. Then you can check if $array[ $id ] is already set. – Tobias Oct 10 '11 at 11:16
  • Code here: http://stackoverflow.com/questions/7711829/check-if-the-value-in-array-is-unique-or-duplicated-php/7711963#7711963 – hakre Oct 10 '11 at 11:17
0
<?php
$file = "student.txt";
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);

$list = array();

$list = explode("\n",$contents);
$list = array_map("trim", $list);

$current = "foo@bar.com";

echo in_array($current,$list) ? $current.' exists' : $current.' does not exist';
?>
rahularyansharma
  • 11,156
  • 18
  • 79
  • 135
  • I think you should know about the [`file` function](http://php.net/manual/en/function.file.php) in PHP. – hakre Oct 10 '11 at 11:14
0

Iterate over the input array, get the ID per item, if the ID is new, store the ID, if not, give error:

$filename = 'students.txt';

$array = file($filename, FILE_IGNORE_NEW_LINES);

$unique = array();
foreach($array as $line => $student)
{
    $r = preg_match('/ (\d+)$/', $student, $matches);
    if (!$r) continue;
    list(,$id) = $matches;
    if (isset($unique[$id]))
        printf("Duplicate ID found (%d) in '%s' line %d.\n", $id, $student, $line);
    else
        $unique[$id] = 1;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
0

try this:

$ids = array();
$users = fopen("ciccio.txt", "r"); //open file
while (!feof($users)) {
    $line = fgets($users, 4096); //read one line
    list ($firstname, $lastname, $id) = explode(" ", $line);
    $id = (int)$id;
    if (empty($ids)) {
        $ids[] = $id;
    } else {
        if (in_array($id, $ids)) {
            echo "Duplicate ID: " . $id . "<br/>";
        } else {
            $ids[] = $id;
        }
    }
}
fclose($users); #close file
JellyBelly
  • 2,451
  • 2
  • 21
  • 41
  • Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\AppServ\www\hw\sort11.php on line 8 – wael Oct 10 '11 at 11:31
  • the browser don't give me any output!! do you know why? – wael Oct 10 '11 at 11:52