-1

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?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
UmarAbbasKhan
  • 91
  • 1
  • 12
  • Better still show us how this array is created and we can fix it at source rather than frig it after some mistake was made... _Good coding practice by the way, fix error at source_ – RiggsFolly Oct 10 '16 at 17:19
  • Please show us the exact output from `var_dump(PInvoicesList);`. `array_unique()` is the correct function, but your two values are not the same. – Rizier123 Oct 10 '16 at 17:23
  • @RiggsFolly thats because this code is getting data from database and this data is actually present in database. – UmarAbbasKhan Oct 10 '16 at 17:29
  • 3
    Regardless of what else is going on, `array_unique($PInvoicesList);` _will not change_ `$PInvoicesList`. `array_unique` _returns_ a new array with the distinct array. It doesn't affect the original array. – Don't Panic Oct 10 '16 at 17:30
  • 4
    FIX: `SELECT DISTINCT DocumentID FROM AllDataEntriesList` You 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 place___ – RiggsFolly Oct 10 '16 at 17:30
  • ^^^^^^ This ^^^^^^ And the one before. – AbraCadaver Oct 10 '16 at 17:32
  • 1
    I hope by pointing that out about `array_unique` that I did not appear to be advocating that as a good solution to this problem. – Don't Panic Oct 10 '16 at 17:35

1 Answers1

0

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);
Chandana Kumara
  • 2,485
  • 1
  • 22
  • 25