[英]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? 我想通过nr删除项目[3] => Array(),就像未设置[nr] => 3一样,那我该怎么办?
You have to loop over members items and check if nr
has the value you want. 您必须遍历成员项,并检查nr
是否具有所需的值。 Then, you could use unset()
to remove the entry : 然后,您可以使用unset()
删除条目:
foreach ($array['members'] as $key => $item) {
if (isset($item['nr']) && $item['nr'] == 3) {
unset($array['members'][$key]) ;
}
}
You can directly use unset($main['members'][2]['nr']);
您可以直接使用unset($main['members'][2]['nr']);
if you dont want to use foreach loop 如果您不想使用foreach循环
I would use array_filter instead of unset I'd filter the values that you actually need. 我会使用array_filter而不是unset来过滤您实际需要的值。
$array['members'] = array_filter($array['members'], function($member) {
return $member['nr'] !== 3;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.