1

How to ignore if array(0) is null php

When i am print the value the array is like this: Array ( [0] => )

if i print count($testArray) it is showing 1

But if the array value is comes like this i don't want to insert it into the database. Please suggest how can i do this.

Update:

var_dump result: array(1) { [0]=> string(0) "" }

2
  • empty($testArray[0]) should return true, then you can skip, otherwise (if $testArray[0] has any kind of value other than null-values) it returns false. Commented Mar 27, 2012 at 14:12
  • var_dump result: array(1) { [0]=> string(0) "" } Commented Mar 27, 2012 at 14:17

5 Answers 5

8

Use this

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

Comments

0
if (!empty($testArray[0]))
    // do something

OR

foreach ($testArray as $value)
{
    if (!empty($value))
    {
        // do something
    }
}

Comments

0

This would help you filter the array

    $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5,"f"=>"","i"=>0);
    function filter($var)
    {
        return !empty($var);
    }
    var_dump(array_filter($array1, "filter"));

Output

    array
      'a' => int 1
      'b' => int 2
      'c' => int 3
      'd' => int 4
      'e' => int 5

Comments

0

You can try

if(!isset($testArray[0]))
{
    //here the zero key has null value
}

or

if($testArray[0] == NULL)
{
    //here the zero key has null value
}

But try first:

var_dump($testArray);

And let us know what it's shown.

2 Comments

var_dump result: array(1) { [0]=> string(0) "" }
Then the array value is not null, it's an empty string. Try: if($testArray[0] == '')
0

I was trying to ignore an empty value but some of the codes didn't work. So I tried this and works, maybe this code will help someone out there.

if (empty($YourValue)){ continue; } else { echo $YourValue; }

The code continue skips your empty value.

It helps me creating a navbar inside <li> ... </li> code for some reason.

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.