php

How to remove null or empty values from an array in PHP

In this tutorial, we are going to see how to remove null or empty values from an array in PHP. You can use the PHP array_filter() function to remove or filter empty values from an array. This function usually filters the values of an array using a callback function. However, if no callback is specified, all values in the array equal to FALSE will be removed, such as an empty string or a NULL value.
 

How to remove null or empty values from an array in PHP
<?php
	$arr = array("", "lorem", 0, null, "ipsum", false);

	// Filter the array
	$new_arr = array_filter($arr);                 
	print_r($new_arr);
?>

Output:

Array ( 
	[1] => lorem 
	[4] => 1 
	[5] => ipsum 
)
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *