Home > Php programming > Remove Empty/Null Rows from array using PHP

Remove Empty/Null Rows from array using PHP

Hi Friends,

I got so many empty element as well as null values in array and my SQL query is some what complex so i was not able to use IS NOT NULL in mysql Query. Only alternative way i have is removing this null values from array using PHP. I used is_null function of PHP and remove null elements from array. Be careful with “” and null as they are two different things. I you want to remove empty elements as well than you can use one more condition along with is_null. Look at below code. I hope this helps you a little.

if(!empty($rec))
{
 foreach ($rec as $key=>$value)  
 {
  if (is_null($value['column_name']))
  { // Check for null value of column
   unset($rec[$key]); // remove row from array if null element found
  }
 }
}

Let me know if you have any problem.

Advertisements
  1. June 15, 2011 at 8:28 pm

    thanks bhavin !

  2. June 15, 2011 at 8:29 pm

    Thanks a lot !!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: