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?

Dobi Caps
  • 57
  • 1
  • 8

3 Answers3

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

Robert
  • 19,800
  • 5
  • 55
  • 85
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]) ;
    }
}
Syscall
  • 19,327
  • 10
  • 37
  • 52
1

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

PPL
  • 6,357
  • 1
  • 11
  • 30