Php 是否从foreach中删除索引数组?

Php 是否从foreach中删除索引数组?,php,Php,简单问题;如何从foreach()中删除正确的数组 unset($e)似乎不能正常工作。从正确索引中删除正确数组的正确解决方案是什么?在foreach中执行取消设置($e)会取消设置变量$e,而不是它所代表的数组中的项。为此,您需要使用常规for循环 for($i = 0; $i < count($items); $i++) { if ($items[$i]['seat'] == $users[$clientID]['seat']) { unset($items[$i])

简单问题;如何从foreach()中删除正确的数组

unset($e)似乎不能正常工作。从正确索引中删除正确数组的正确解决方案是什么?

foreach
中执行
取消设置($e)
会取消设置变量
$e
,而不是它所代表的数组中的项。为此,您需要使用常规for循环

for($i = 0; $i < count($items); $i++) {
   if ($items[$i]['seat'] == $users[$clientID]['seat']) {
      unset($items[$i])
   }
}
for($i=0;$i
foreach
中执行
取消设置($e)
将取消设置变量
$e
,而不是它所表示的数组中的项。为此,您需要使用常规for循环

for($i = 0; $i < count($items); $i++) {
   if ($items[$i]['seat'] == $users[$clientID]['seat']) {
      unset($items[$i])
   }
}
for($i=0;$i
尝试以下方法:

foreach ($items as &$e):
    if ($e['seat'] == $users[$clientID]['seat']):
        //It's done, delete it.
        unset ($e);
    endif;
endforeach;
尝试以下方法:

foreach ($items as &$e):
    if ($e['seat'] == $users[$clientID]['seat']):
        //It's done, delete it.
        unset ($e);
    endif;
endforeach;

这是xbonez给出的for循环的另一种选择,通过传递
值:

foreach ( $items as $key => $e):
    if ( $e['seat'] == $users[$clientID]['seat']):
        //It's done, delete it.
        unset ( $items[$key] );
    endif;
endforeach;

我更喜欢这个版本,但这并不重要

这是xbonez给出的for循环的替代方法,通过传递
值:

foreach ( $items as $key => $e):
    if ( $e['seat'] == $users[$clientID]['seat']):
        //It's done, delete it.
        unset ( $items[$key] );
    endif;
endforeach;
我更喜欢这个版本,但这并不重要

由于您的答案仅限于非关联数组,因此($arr as$key=>$val)
的可能副本的可能副本会更好。
对于($arr as$key=>$val)
的可能副本会更好,因为您的答案仅限于非关联数组。