I have a database with multiple entries. One specific column of entries I am interested in is called sample_group
. If there are 10 rows of entries in sample_group
, I am trying to create a array of the unique entries using PHP.
For example, the entries would be "food", "food", "water", "food", "swabs", "swabs", "swabs", "food", "water", "water"
and after creating an array should be: array("food", "water", "swabs")
.
Here is my attempted code, it sort of works but the array has an empty entry at the end:
$sql3 = "SELECT * FROM samples_database WHERE order_id=$order_id;";
$result3 = mysqli_query($conn, $sql3);
$group_array = '';
while ($input = mysqli_fetch_array($result3)) {
$group_array .= $input['sample_group'] . ',';
}
$group_array2 = array_filter(array_unique(explode(',', $group_array)));
And then the foreach
loop:
foreach ($group_array2 as $group) {
//do something
}
Can anybody please push me in the right direction?