Comment supprimer des éléments de tableau multidimensionnel par valeur en PHP ?

Mary-Kate Olsen
Libérer: 2024-10-18 11:01:03
original
126 Les gens l'ont consulté

How to Remove Multidimensional Array Elements by Value in PHP?

Deleting Elements from a Multidimensional Array Based on Value

In PHP, working with multidimensional arrays often presents the need to remove specific elements based on their values. Here's how you can approach this challenge:

Solution:

To remove elements from a multidimensional array based on a value, you can use the following function:

<code class="php">function removeElementWithValue($array, $key, $value) {
    foreach ($array as $subKey => $subArray) {
        if ($subArray[$key] == $value) {
            unset($array[$subKey]);
        }
    }
    return $array;
}</code>
Copier après la connexion

Usage:

To utilize this function, you simply need to pass in the array, the key of the element you want to match, and the value to match. For example, in your case, you would call the function like this:

<code class="php">$array = removeElementWithValue($array, "year", 2011);</code>
Copier après la connexion

This will remove any sub-arrays where the "year" key has the value "2011" from the given array.

Output:

After executing the above function call, your array will look like this:

Array
(
    [0] =&gt; Array
        (
            [filmId] =&gt; 68593
            [url] =&gt; http://www.moviemeter.nl/film/68593
            [title] =&gt; Unstoppable
            [alternative_title] =&gt; 
            [year] =&gt; 2010
            [thumbnail] =&gt; http://www.moviemeter.nl/images/covers/thumbs/68000/68593.jpg
            [average] =&gt; 3.3
            [votes_count] =&gt; 191
            [similarity] =&gt; 100.00
            [directors_text] =&gt; geregisseerd door Tony Scott
            [actors_text] =&gt; met Denzel Washington, Chris Pine en Rosario Dawson
            [genres_text] =&gt; Actie / Thriller
            [duration] =&gt; 98
        )
    [1] =&gt; Array
        (
            [filmId] =&gt; 17931
            [url] =&gt; http://www.moviemeter.nl/film/17931
            [title] =&gt; Unstoppable
            [alternative_title] =&gt; Nine Lives
            [year] =&gt; 2004
            [thumbnail] =&gt; http://www.moviemeter.nl/images/covers/thumbs/17000/17931.jpg
            [average] =&gt; 2.64
            [votes_count] =&gt; 237
            [similarity] =&gt; 100.00
            [directors_text] =&gt; geregisseerd door David Carson
            [actors_text] =&gt; met Wesley Snipes, Jacqueline Obradors en Mark Sheppard
            [genres_text] =&gt; Actie / Thriller
            [duration] =&gt; 96
        )
)
Copier après la connexion

The sub-arrays with "year" equal to "2011" have been successfully removed.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!