I believe my issue is fairly basic and simple, but I think I am not getting through it.I have a PHP array which has been dynamically populated with data from the database
$PInvoicesList=array();
$SelectPInvoicesList ="SELECT * FROM AllDataEntriesList
WHERE BusinessID = '{$businessid}'
AND DocumentType = 'purchaseinvoice'
ORDER BY Time ASC;";
$SelectPInvoicesList_query = mysqli_query($connection, $SelectPInvoicesList);
if(!$SelectPInvoicesList_query){
die ("Database query for searching Purchase Invoices failed.".mysqli_error($connection));
}
while ($SelectPInvoicesList_array = mysqli_fetch_assoc($SelectPInvoicesList_query)){
$PInvoicesList[]=$SelectPInvoicesList_array["DocumentID"];
}
array_unique($PInvoicesList);
print_r($PInvoicesList);
And this gives me an array
Array ( [0] => pk-000000003-purchaseinvoice-1
[1] => pk-000000003-purchaseinvoice-1
)
When I apply array_unique($PInvoicesList, SORT_STRING);, I get both of both elements in the array, whereas they both have the same data. I used this function as array_unique($PInvoicesList, SORT_REGULAR); but still both the elements in the array are appearing and they have the exact same data. Can anyone guide me how can I remove the elements with duplicate values from this array?
var_dump(PInvoicesList);.array_unique()is the correct function, but your two values are not the same.array_unique($PInvoicesList);will not change$PInvoicesList.array_uniquereturns a new array with the distinct array. It doesn't affect the original array.SELECT DISTINCT DocumentID FROM AllDataEntriesListYou will only get one and the query will run faster as you are not returning other columns you have no use for Dont fix it, do it right in the first placearray_uniquethat I did not appear to be advocating that as a good solution to this problem.