Remove duplicates using array_unique and printing the result

147 views Asked by At

I've been searching and trying for hours, I want to look for duplicates in my variables which print numbers and then eliminate the duplicates leaving just original numbers.

Here is what I've been trying:

$priceminpps = get_field('price_minpps');
$pricemaxpps = get_field('price_maxpps');
$priceminunit = get_field('price_minunit');
$pricemaxunit = get_field('price_maxunit');

$find_duplicates = array($priceminpps, $pricemaxpps, $priceminunit, $pricemaxunit, );

$result = array_unique($find_duplicates);
print($result);

But it doesn't work, can anyone help?

1

There are 1 answers

2
JustAPirate On

Double check if array_unique doesn't do what it should.

If it's realy array_unqiue() i would generate a new array and push the unique numbers in this new one. You can check if it's already in the new array with in_array(number-to-check, $array).

If in_array() doesn't return true, add it to the unique-number-array.

Example:

$result = array();
foreach ($find_duplicates as $number) {
    if(!in_array($number, $result)) {
        $result[] = $number;
    }
}