-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?

7
  • 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 Commented Oct 10, 2016 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. Commented Oct 10, 2016 at 17:23
  • 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. Commented Oct 10, 2016 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 Commented Oct 10, 2016 at 17:30
  • 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. Commented Oct 10, 2016 at 17:35

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

5 Comments

How should removing the sorting parameter make a difference?
This answer is correct because I was making a mistake trying to change the original array, whereas array_unique makes up another array and puts the data inside that new array.
Dont fix up bad ideas. Do it right to start with
@RiggsFolly I called all the data from database because I needed more attributes for the same documentid at the same time.
@Rizier123 - array_unique() default use SORT_REGULAR for sorting. Therefore no need to add manually. Anyway I removed that condition from my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.