1

I've tried the following to remove the null values from a resultset.

if((is_null($value)) || ('NULL' === $value) || (null === $value))

none seem to be working.

Here's my full code:

// remove null values from contact details
$contactData[$key] = removeEmptyDataFromArray($contact);    
print "<pre>".print_r($contactData[$key],true)."</pre">;

// remove empty data function
function removeEmptyDataFromArray(array $filledData)
{
    foreach ($filledData as $key => $value )
    {
        if ((is_null($value)) || (strlen ( $value ) === 0) || ('NULL' == $value) || (NULL === $value))
        {
            unset ( $filledData [$key] );
        }
    }
    return $filledData;
}

And here are the results from my print_r:

firstname :
lastname :
middlename :
primary_emailaddress : [email protected]
5
  • 1
    var_dump $value to see what exactly it is. Commented Jul 28, 2013 at 19:38
  • my vardump is returning NULL Commented Jul 28, 2013 at 19:41
  • Yes, do a var_dump/print_r to see what exactly is the data you are getting from your query. Then look into empty, is_null to see what serves your purpose. Commented Jul 28, 2013 at 19:41
  • Can you show some more code that you are using? Commented Jul 28, 2013 at 19:41
  • Also, 'NULL' === $value is not the way to check for NULL. Also, make sure you are checking against the correct value, like do you need to use an array index for the particular value, rather than the complete response from query? Commented Jul 28, 2013 at 19:44

2 Answers 2

1

why not remove Null values from your query without php ?

 WHERE your_column is not null

or

WHERE your_column <> '' 

depends on how is your column. you have used the whole function just to remove the null values while in sql is just 5 words

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, why did it not strike me? :)
i cannot exclude rows where one of the columns is null. I need to make sure that i still get the results despite the null value
0

In your code, instead of

if ((is_null($value)) || (strlen ( $value ) === 0) || ('NULL' == $value) || (NULL === $value))

try

if (empty($value))

2 Comments

Yes, and this is no trick :) just clarity of basic concepts of PHP and clarity of requirements. At the risk of offending you, I suggest a read of programming by accident :)
No offense taken. Thanks for the help.

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.