Reputation: 466
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) "" }
Upvotes: 1
Views: 3723
Reputation: 401
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.
Upvotes: 0
Reputation: 516
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.
Upvotes: 0
Reputation: 95151
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
Upvotes: 0
Reputation: 14479
if (!empty($testArray[0]))
// do something
OR
foreach ($testArray as $value)
{
if (!empty($value))
{
// do something
}
}
Upvotes: 0