0

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."'\n";
 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

)

3 Answers 3

4

You should do:

foreach ($attachments as $k=>$v) {
    //...magic
    unset($attachments[$k]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I'm in such a rush, worked! Thanks! Will accept when the limit expires.
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.