2

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?

3 Answers 3

3

I would use array_filter instead of unset I'd filter the values that you actually need.

$array['members'] = array_filter($array['members'], function($member) {
    return $member['nr'] !== 3;
});

Working fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

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]) ;
    }
}

Comments

1

You can directly use unset($main['members'][2]['nr']); if you dont want to use foreach loop

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.