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

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
        )

)

1 answer

  • answered 2022-05-04 10:57 Bhargav Chudasama

    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);
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum