好的。我有一个像这样定义的填充数组:
$subs = array();
for (...) {
// extracting of variables $knowledgeLevel, $name, $short, $long
$subs [] = array (
'knowledgeLevel' => $knowledgeLevel,
'name' => $name,
'shortTheory' => $short,
'longTheory' => $long
);
}
然后我收到一个值$kl
,我想最终从数组$subs
中删除element
等于knowledgeLevel
的{{1}}。我试过这样做:
$kl
等等
foreach ( $subs as $subject ) {
if ($subject['knowledgeLevel'] == $kl) {
unset ($subject);
}
}
但这些似乎不起作用。
答案 0 :(得分:3)
为了能够直接修改循环内的数组元素 使用&前面的$ value。在这种情况下,值将由 参考
将&
与foreach ( $subs as &$subject )
foreach ( $subs as &$subject )
if ( $subject['knowledgeLevel'] == $kl )
unset ($subject);
或试试这个:
此表单还会将当前元素的键分配给$键 每次迭代都有变量。
foreach ( $subs as $key => $subject )
if ( $subject['knowledgeLevel'] == $kl )
unset( $subs[$key] );
答案 1 :(得分:0)
$subject
未引用$subs
它的新变量,unset($subject['anything'])
您没有删除$subs
使用for
循环:
for($i=0;$i<count($subs);$i++) {
if ($subs[$i]['knowledgeLevel'] == $kl) {
unset ($subs[$i]);
}
}