开发者

How to filter array values from another arrays values and return new array? [duplicate]

开发者 https://www.devze.com 2023-04-01 12:02 出处:网络
This question already has answers here: Remove item from array if it exists in a 'disallowed words' array
This question already has answers here: Remove item from array if it exists in a 'disallowed words' array (2 answers) Closed 5 months ago.

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 开发者_如何学Pythonlanguages 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!


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

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

PHP Manual (array_diff)


The array_diff function will work for you. http://php.net/manual/en/function.array-diff.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号