1

I want to remove empty values from an array. I have tried array_filter, array_diff and a custom coded function but nothing helped.

Here's my code that generates the array:

for($i=0;$i<$_POST['size'];$i++)
{
    $e = array();
    if(!@strcasecmp("yes", $_POST['email'.$i]))
    {
        //array_push($e, $_POST['u'.$i]);
        $e[$i] = $_POST['u'.$i];
    }
    else
    {
        $e[$i] = "";
    }
    //var_dump($e);

var_dump($e);
}

My var_dump outputs:

array (size=1)
  0 => string '12' (length=2)
string '12' (length=2)
array (size=1)
  1 => string '13' (length=2)
string '13' (length=2)
array (size=1)
  2 => string '' (length=0)
string '' (length=0)
array (size=1)
  3 => string '' (length=0)
string '' (length=0)
array (size=1)
  4 => string '' (length=0)
string '' (length=0)
array (size=1)
  5 => string '' (length=0)

How would I remove these empty indexes? Any suggestions are welcome.

5
  • do the key associations need to remain? Commented Jul 20, 2014 at 21:22
  • @Dragon I don't think so. I only need the value 12,13 in the array..so I could later use implode and then convert it to a string. Commented Jul 20, 2014 at 21:23
  • Are you initialising that array in the right place? You are resetting the ith value in each iteration, but it is being wiped at every iteration too. Looks like that initialisation should go before the loop? Commented Jul 20, 2014 at 21:29
  • I'm guessing the else clause is the problem - that seems to be what is adding the last four empty-string entries. Remove it and see if that helps? Commented Jul 20, 2014 at 21:30
  • OP, use tab completion to get your reply-handles correct. The above is @Dagon, not Dragon. Commented Jul 20, 2014 at 22:37

3 Answers 3

1

The elements you want to remove are not null, they are empty strings. You can still use array_filter like the following

$filtered_array = array_filter($e, 'strlen');

We use strlen as the callback function because empty string return 0 and is evaluated to false in PHP. Actually the following also works as pointed out by @revo:

$filtered_array = array_filter($e);
Sign up to request clarification or add additional context in comments.

1 Comment

@revo, thanks for pointing it out. I just assumed it didn't work because OP said it didn't...
1

You can use name="email[]" to get an indexed array from your POST, so you can drop 'size' and just foreach over the array instead. Move the initalization of $e out, as you're resetting it for each iteration.

if (isset($_POST['email']) && is_array($_POST['email'])) {
    $e = array();

    foreach ($_POST['email'] as $email) {
        if (trim($email)) {
            $e[] = $email;
        }
    }

    var_dump($e);
}

.. if you really want to keep the worse method with 'size' and for, remove the is_array check and replace with your own loop structure, but keep the internal checks and move $e initialization out from your for loop.

Comments

0

What is the point of the else condition? Simply omit it:

for($i=0;$i<$_POST['size'];$i++)
{
    $e = array();
    if(!@strcasecmp("yes", $_POST['email'.$i]))
    {
        //array_push($e, $_POST['u'.$i]);
        $e[] = $_POST['u'.$i];
    }

var_dump($e);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.