Home > OS >  Why I am getting single value in after using array_unique()?
Why I am getting single value in after using array_unique()?

Time:05-04

I have this array...I am trying to using array_unique , how can i achieve that?

Array
(
    [0] => Array
        (
            [referrer_id] => usr1573
        )

    [1] => Array
        (
            [referrer_id] => usr1f0b4
        )

    [2] => Array
        (
            [referrer_id] => usr1573
        )

)

After using array_unique there is one data showing.

    Array
(
    [0] => Array
        (
            [referrer_id] => usr1573
        )

)

I want to show the output like this. Which is obvious.

Array
(
    [0] => Array
        (
            [referrer_id] => usr1573
        )

    [1] => Array
        (
            [referrer_id] => usr1f0b4
        )

)

CodePudding user response:

because you have multidimensional array, array_unique is used with a single array

try with this code.

$array = array(
    [
        'referrer_id' => 'usr1573',
    ],
    [
        'referrer_id' => 'usr1f0b4'
    ],
    [
        'referrer_id' => 'usr1573'
    ]

);
$newArray = array_map("unserialize", array_unique(array_map("serialize", $array)));
print_r($newArray);
  • Related