请注意,`array_intersect` 和 `array_unique` 不适用于多维数组。
例如,如果您有以下数组:
<?php
$orders_today[0] = array('John Doe', 'PHP Book');
$orders_today[1] = array('Jack Smith', 'Coke');
$orders_yesterday[0] = array('Miranda Jones', 'Digital Watch');
$orders_yesterday[1] = array('John Doe', 'PHP Book');
$orders_yesterday[2] = array('Z? da Silva', 'BMW Car');
?>
并且您想了解今天和昨天是否有人购买了相同的东西,并使用 `array_intersect($orders_today, $orders_yesterday)`,您将得到以下结果:
<?php
Array
(
[0] => Array
(
[0] => John Doe
[1] => PHP Book
)
[1] => Array
(
[0] => Jack Smith
[1] => Coke
)
)
?>
但是,我们可以通过序列化内部数组来解决这个问题。
<?php
$orders_today[0] = serialize(array('John Doe', 'PHP Book'));
$orders_today[1] = serialize(array('Jack Smith', 'Coke'));
$orders_yesterday[0] = serialize(array('Miranda Jones', 'Digital Watch'));
$orders_yesterday[1] = serialize(array('John Doe', 'PHP Book'));
$orders_yesterday[2] = serialize(array('Z? da Silva', 'Uncle Tungsten'));
?>
这样,`array_map("unserialize", array_intersect($orders_today, $orders_yesterday))` 将返回:
<?php
Array
(
[0] => Array
(
[0] => John Doe
[1] => PHP Book
)
)
?>
显示了谁在今天和昨天购买了相同的东西 =)
[]s