How can PHP be optimized to efficiently display unique entries in a select list without duplicates?
To efficiently display unique entries in a select list without duplicates in PHP, you can use an array to store the unique values and then iterate through the original list to check for duplicates before adding them to the array. This way, you can ensure that only unique entries are displayed in the select list.
<?php
// Original list of entries
$entries = ["Apple", "Banana", "Orange", "Apple", "Grapes", "Banana"];
// Array to store unique entries
$uniqueEntries = [];
// Iterate through original list to check for duplicates and add unique entries to the array
foreach ($entries as $entry) {
if (!in_array($entry, $uniqueEntries)) {
$uniqueEntries[] = $entry;
}
}
// Display unique entries in a select list
echo "<select>";
foreach ($uniqueEntries as $uniqueEntry) {
echo "<option value='$uniqueEntry'>$uniqueEntry</option>";
}
echo "</select>";
?>