我想在PHP中获得一个解决方案,以获得基于子阵列基础的独特阵列.像这样
Array
(
[0] => Array
(
[0] => 1227
[1] => 146
[2] => 1
[3] => 39
)
[1] => Array
(
[0] => 1227
[1] => 146
[2] => 1
[3] => 39
)
[2] => Array
(
[0] => 1228
[1] => 146
[2] => 1
[3] => 39
)
)
至
Array
(
[0] => Array
(
[0] => 1227
[1] => 146
[2] => 1
[3] => 39
)
[1] => Array
(
[0] => 1228
[1] => 146
[2] => 1
[3] => 39
)
)
我的意思是说数组[1]应该被删除,因为数组[0]和数组[1]是相同的.我尝试使用array_unique,但它对我不起作用.
解决方法:
这可以使用array_unique完成,但您还需要使用SORT_REGULAR(PHP 5.2.9)标志:
$array = array(
array(1227, 146, 1, 39),
array(1227, 146, 1, 39),
array(1228, 146, 1, 39),
);
$array = array_unique($array, SORT_REGULAR);
输出:
Array
(
[0] => Array
(
[0] => 1227
[1] => 146
[2] => 1
[3] => 39
)
[2] => Array
(
[0] => 1228
[1] => 146
[2] => 1
[3] => 39
)
)
对于旧版本的PHP,您可以在问题的评论中使用我链接到的solution:
$array = array_map("unserialize", array_unique(array_map("serialize", $array)));
希望这可以帮助!
标签:php,arrays
来源: https://codeday.me/bug/20190713/1454538.html