Search code examples
phparraysarray-unset

How to unset array inside array


I have an array in the following structure

Array ( [members] => Array ( 
    [0] => Array ( [nr] => 2 [email] => email1 ) 
    [1] => Array ( [nr] => 6 [email] => email2 ) 
    [2] => Array ( [nr] => 3 [email] => email3 ) 
 )
 [title] => List members 
)

I want to delete items [3] => Array () by nr, like unset [nr] => 3, so how should I do this?


Solution

  • You have to loop over members items and check if nr has the value you want. Then, you could use unset() to remove the entry :

    foreach ($array['members'] as $key => $item) {
        if (isset($item['nr']) && $item['nr'] == 3) {
            unset($array['members'][$key]) ;
        }
    }