Search code examples
phparraysarray-unique

PHP Array Unique


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?


Solution

  • The array_unique() removes duplicate values from an array. Just use array_unique()

    Refer this http://php.net/manual/en/function.array-unique.php

    $result = array_unique($PInvoicesList);