比较具有不同数量元素的两个多维数组
I have two multidimensional arrays with different number of elements:
$complete = array(array("24","G:\TVShows\24"),array("Lost","G:\TVShows\Lost"),array("Game of Thrones","G:\TVShows\Game of Thrones"));
$subset = array(array("24","G:\TVShows\24","English"));
The first one ($complete) is the complete list of my tv shows on disk (name of the show, path to files). The second one ($subset) come from my database and include the spoken language as a third column / element.
I would like to return the shows that I have on disk but that do not appear in databse. How can I compare those to array with different number of elements?
Thank you for your help!
我有两个具有不同元素数的多维数组: p>
$ complete = array(array(“24”,“G:\ TVShows \ 24”),array(“Lost”,“G:\ TVShows \ Lost”),数组(“权力的游戏”,“G:\ TVShows \权力的游戏“));
$ subset = array(array(”24“,”G:\ TVShows \ 24“,”English“));
code> pre>
\ n 第一个($ complete)是我在磁盘上的电视节目的完整列表(节目名称,文件路径)。 第二个($ subset)来自我的数据库,并将口语作为第三列/元素包含在内。 p>
我想返回我在磁盘上但没有的节目 出现在数据库中。 如何将这些元素与具有不同元素数量的数组进行比较? p>
感谢您的帮助! p>
div>
Since its a multi leveled array, you could use and combine array_map()
and serialize()/unserialize()
. Consider this example:
$complete = array(
array("24","G:\TVShows\24"),
array("Lost","G:\TVShows\Lost"),
array("Game of Thrones","G:\TVShows\Game of Thrones"),
array("The Walking Dead","G:\TVShows\The Walking Dead"),
array("Breaking Bad","G:\TVShows\Breaking Bad"),
array("Heroes","G:\TVShows\Heroes"),
);
$subset = array(
array("24","G:\TVShows\24","English"),
array("The Walking Dead","G:\TVShows\The Walking Dead","English"),
array("Heroes","G:\TVShows\Heroes","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);
Edit: For case insensitive comparisons, you may use this alternative:
$shows_not_in_db = array_map('unserialize', array_udiff($complete, $subset, 'strcasecmp'));
// sample: The walking dead - The Walking Dead