Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicated 2-dimension array in PHP?

I have some idea from the previous posts that were talking about making a hash value for each $arr[$i], and then compare the hash to get the unique array, but I don't know what I can do exactly.

My sample array Data:

$arr = [
    [0, 1, 2, 3],
    [4, 5, 2, 1],
    [0, 0, 0, 0],
    [0, 1, 2, 3]
];

I expected to return:

[
    [0, 1, 2, 3],
    [4, 5, 2, 1],
    [0, 0, 0, 0]
]
like image 939
Jay Avatar asked Dec 07 '22 06:12

Jay


1 Answers

Quick and simple:

$arr = array_map('unserialize', array_unique(array_map('serialize', $arr)));
like image 126
Alix Axel Avatar answered Dec 31 '22 23:12

Alix Axel