How can PHP developers avoid repeating the same entry when reading data line by line from a text file?

When reading data line by line from a text file in PHP, developers can avoid repeating the same entry by storing each entry in an array and checking if the entry already exists before processing it. This can be achieved by using the in_array() function to check if the entry is already in the array before adding it.

// Open the text file for reading
$file = fopen("data.txt", "r");

// Initialize an empty array to store unique entries
$entries = array();

// Read the file line by line
while(!feof($file)) {
    $entry = trim(fgets($file)); // Read and trim each line
    if(!in_array($entry, $entries)) { // Check if entry is not already in the array
        $entries[] = $entry; // Add entry to the array if it is unique
        // Process the unique entry here
    }
}

// Close the file
fclose($file);