如何从另一个数组值滤波器阵列值,并返回新的数组? [英] How to filter array values from another arrays values and return new array?

查看:234
本文介绍了如何从另一个数组值滤波器阵列值,并返回新的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组: $ all_languages​​ $ taken_languages​​ 。一个包含了所有语言(如200或东西),但第二个 - 这已经被选择之前(从0到200)语言

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).

我要删除已拍摄( $ taken_languages​​ )从 $ all_languages​​ 所有的语言,并返回新数组 - $ available_languages​​

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;

}

谢谢指教!

推荐答案

PHP有一个内置该功能(和只是一切:P)

PHP has a built in function for this (and just about everything else :P)

$available_languages = array_diff($all_languages, $taken_languages);

PHP手册(和array_diff)

这篇关于如何从另一个数组值滤波器阵列值,并返回新的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆