如何从另一个数组值中过滤数组值并返回新数组?
我有两个数组:$all_languages
和 $taken_languages
。第一个包含所有语言(例如 200 种或其他语言),第二个包含之前选择的语言(从 0 到 200 种)。
我需要从 $all_languages
中删除已采用的所有语言 ($taken_languages
) 并返回新数组 - $available_languages
。
我的解决方案是两个循环,但是,首先,它不能按预期工作,其次 - 它“不酷”,我相信有更好的解决方案!你能指出我正确的道路吗?
这是我以前做过的,但是,正如我所说,它没有按预期工作......
if (!empty($taken_languages)) {
foreach ($all_languages as $language) {
foreach ($taken_languages as $taken_language) {
if ($taken_language != $language) {
$available_languages[] = $language;
break;
}
}
}
} else {
$available_languages = $all_languages;
}
感谢您的建议!
I have two arrays: $all_languages
and $taken_languages
. One contains all languages (like 200 or something), but second - languages that have been chosen before (from 0 to 200).
I need to remove all languages that have been taken ($taken_languages
) from $all_languages
and return new array - $available_languages
.
My solution was two loops, but, first, it doesn't work as expected, second - it's 'not cool' and I believe that there are better solutions! Can you point me to the correct path?
This is what I have done before, but, as I said, it doesn't work as expected...
if (!empty($taken_languages)) {
foreach ($all_languages as $language) {
foreach ($taken_languages as $taken_language) {
if ($taken_language != $language) {
$available_languages[] = $language;
break;
}
}
}
} else {
$available_languages = $all_languages;
}
Thanks in advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 为此有一个内置函数(以及几乎所有其他内容:P)
PHP 手册 (array_diff)
PHP has a built in function for this (and just about everything else :P)
PHP Manual (array_diff)
array_diff
函数将适合您。http://php.net/manual/en/function.array-diff.php
The
array_diff
function will work for you.http://php.net/manual/en/function.array-diff.php