Archive for July, 2009

Removing null values from a PHP Array

Wednesday, July 29th, 2009

The following function will remove all null values from a PHP array. This is especially useful when exploding strings with a post/prefixed delimiter.

function array_remove_null(&$array){
	$new_array = array();
	foreach($array as $k=>$v){
		if($v) $new_array[$k]=$v;
	}
	$array=$new_array;
}

To use the function simply follow the example below;

array_remove_null($your_array_object);

© 2006 - 2010 Matt Lowden. Creative Commons all rights reserved.