PHP取消设置数组键,空值不起作用

For some reason I can not determine why empty array keys aren't being unset. Here is what I have...

PHP

<?php
$attachments = explode('|',$_POST['post_attachments']);
foreach($attachments as $k=>$v)
{
echo 'k = \''.$v."'
";
 if ($v=='')
 {
  unset($k);
 }
}
print_r($attachments);die();
?>

Output

k = ''

k = 'secret_afound.gif'

k = 'secret_aunlocked.gif'

Array (

[0] => 
[1] => secret_afound.gif
[2] => secret_aunlocked.gif

)

You should do:

foreach ($attachments as $k=>$v) {
    //...magic
    unset($attachments[$k]);
}

You are only unsetting $k, not the element in attachments. Try unset($attachments[$k]);

I believe you should use unset($attachments[$k]);.

In this scenario I like to think of $k as a temporary variable. Even though you unset it you didn't alter $attachments in anyway.