比较两个元素数量不同的多维数组



我有两个元素数量不同的多维数组:

$complete = array(array("24","G:TVShows24"),array("Lost","G:TVShowsLost"),array("Game of Thrones","G:TVShowsGame of Thrones"));
$subset = array(array("24","G:TVShows24","English"));

第一个($complete)是磁盘上电视节目的完整列表(节目名称,文件路径)。第二个($子集)来自我的数据库,并将口语作为第三列/元素。

我想返回我在磁盘上的节目,但不出现在数据库中。我如何将它们与具有不同数量元素的数组进行比较?

谢谢你的帮助!

由于它是一个多层数组,您可以使用并组合array_map()serialize()/unserialize()。考虑这个例子:

$complete = array(
    array("24","G:TVShows24"),
    array("Lost","G:TVShowsLost"),
    array("Game of Thrones","G:TVShowsGame of Thrones"),
    array("The Walking Dead","G:TVShowsThe Walking Dead"),
    array("Breaking Bad","G:TVShowsBreaking Bad"),
    array("Heroes","G:TVShowsHeroes"),
);
$subset = array(
    array("24","G:TVShows24","English"),
    array("The Walking Dead","G:TVShowsThe Walking Dead","English"),
    array("Heroes","G:TVShowsHeroes","English"),
);
$shows_not_in_db = array();
// properly format the subsets for comparison on complete
foreach($subset as $key_s => $value_s) {
    array_pop($value_s); // remove the last element "English"
    $subset[$key_s] = serialize($value_s);
}
// serialize each complete arrays
$complete = array_map('serialize', $complete);
$shows_not_in_db = array_map('unserialize', array_diff($complete, $subset)); // array diff them, then unserialize
print_r($shows_not_in_db);

编辑:对于不区分大小写的比较,您可以使用以下选项:

$shows_not_in_db = array_map('unserialize', array_udiff($complete, $subset, 'strcasecmp'));
// sample: The walking dead - The Walking Dead

相关内容

  • 没有找到相关文章

最新更新