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;	
	}
	
}

Recommended Answers

All 4 Replies

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

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

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

And call it on line 22.

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

Thanks,
Marais

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.