Archive

Posts Tagged ‘Remove Empty/Null Rows from array using PHP’

Remove Empty/Null Rows from array using PHP

June 1, 2011 2 comments

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
%d bloggers like this: