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.

1
  • do u want to remove duplicates silently?? Commented Oct 10, 2011 at 11:04

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\AppServ\www\hw\sort11.php on line 8
the browser don't give me any output!! do you know why?
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];

4 Comments

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
I created this code to read but how to save, and if one ID is already saved throw error?
Just save the id in an array. Then you can check if $array[ $id ] is already set.
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 = "[email protected]";

echo in_array($current,$list) ? $current.' exists' : $current.' does not exist';
?>

1 Comment

I think you should know about the file function in PHP.
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.