0

Hi all,

I have just coded this for my website, is there a way to improve this either by extending for a more general use or just improving my logic's. Thank you in advance.

function removeEmptyArray($array, $remove_null_number = true) {
	$new_array = array();

	$null_exceptions = array();

	foreach ($array as $key => $value) {
		$value = trim($value);

        if($remove_null_number) {
	        $null_exceptions[] = '0';
		}

        if(!in_array($value, $null_exceptions) && $value != "") {
            $new_array[] = $value;
        }
    }
    return $new_array;
}

function ifValue($value, $suffix="", $prefix="", $returnvalue=true) {
	
	$value = removeEmptyArray($value);
	
	if (!empty($value)) {
		foreach ($value as $item) {
			$return .= $item." ";	
		}
		if ($returnvalue) {
			return $prefix.trim($return).$suffix;	
		} else {
			return $prefix.$suffix;	
		}
	} else {
		$return = "";
		return $return;	
	}
	
}
2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by scarcella
0

For the remove empty function, try using the array_filter method. It removes zero and null values.

0

Thanks for the prompt answer, where did you want me to use that method? Not sure as to where i should place that, and what is the syntax?

Thanks,
Marais

1

$array = array_filter($array); Find more info in the PHP manual.

And call it on line 22.

0

Thank you, makes sense. So i remove the removeEmptyArray function, and just use array_filter?

Thanks,
Marais

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.