ホームページ  >  記事  >  バックエンド開発  >  値に基づいて多次元配列から要素を削除するにはどうすればよいですか?

値に基づいて多次元配列から要素を削除するにはどうすればよいですか?

Mary-Kate Olsen
Mary-Kate Olsenオリジナル
2024-10-18 12:23:30628ブラウズ

How to Remove Elements from Multidimensional Arrays Based on a Value?

Removing Elements from Multidimensional Arrays Based on Value

To remove elements from a multidimensional array based on a specific value, you can utilize the following method:

function removeElementWithValue($array, $key, $value) {
    foreach ($array as $subKey => $subArray) {
        if ($subArray[$key] == $value) {
            unset($array[$subKey]);
        }
    }
    return $array;
}

To utilize this function, pass in the multidimensional array, the key you're matching against, and the value you want to remove. For instance, to remove all sub-arrays where the "year" key has a value of 2011, call the function as follows:

$array = removeElementWithValue($array, "year", 2011);

This will modify the original $array by eliminating any sub-arrays meeting the specified criteria.

以上が値に基づいて多次元配列から要素を削除するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。