Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

david001's avatar

How to remove duplicate data from array

Hi, i have 3 queries and i used array_push() to push all three result in array for example:


$data = [ ];

$q1=Model::get()
array_push($data,$q1);

$q2=Model::get();
array_push($data,$q2);

$q3=Model::get();
array_push($data,$q3);

AND return $data gives following result


[
[
{
    //data
]
]

I used array_unique , but its not working. i can't filter array. i can't remove duplicate data in array. Please help me. Thanks

0 likes
2 replies
mstrauss's avatar

Seems like your result is a multidimensional array, so you may have to customize the array_unique method a bit, per the PHP Docs:

Note: Note that array_unique() is not intended to work on multi dimensional arrays.

That being said, despite the documentation note, if your PHP version is >= 7, you should be able to use array_unique with the SORT_REGULAR flag as a second parameter. Like

$unique_array = array_unique($array, SORT_REGULAR);

Note that the above will only remove array items that have the same keys and values. For example:

   [
        [
            0 => 'value1'
        ],
        [
            1 => 'value2'
        ],
        [
            2 => 'value1'
        ],

    ];

All of these array items would be kept since the keys and values are not identical.

But in the following array, index 2 should be removed because it is a duplicate:

   [
        [
            0 => 'value1'
        ],
        [
            1 => 'value2'
        ],
        [
            0 => 'value1'
        ],

    ];

Please or to participate in this conversation.