• Calculating...
  • 5 years ago
  • 6.9K Views

PHP Program to remove empty or specific array elements

This tutorial is created to remove specific or empty (NULL) array elements from the array.

  1. array_filter() to Remove Empty Elements : array_filter() function removes false values when declared using a callback function, however, If the callback function returns true, the current value from input is returned into the result array.
  1. $array (mandatory): This is where the input array is given.
  2. $callback_function (optional): The user-defined function is given . If the function is not given then all entries of the array equal to FALSE and will be removed.
  3. $flag (optional) : The arguments passed to the callback function is defined.
    • ARRAY_FILTER_USE_KEY – Only key argument is passed to the callback function, instead of the value of the array.
    • ARRAY_FILTER_USE_BOTH – Both value and key as arguments to callback instead of the value.

Syntax :

array array_filter($array, $callback_function, $flag)

Below is a program showing how to return filtered array elements using array_filter() function.

<?php
// Declaring the array
$array = array("bishrul", 27, '', null, "developer", 45,"for", 1994, false,);

// Function to remove empty elements
$filtered_array = array_filter($array);

// Display the array
var_dump($filtered_array);
 

Output :

array(6) { [0]=> string(7) "bishrul" [1]=> int(27) [4]=> string(9) "developer" [5]=> int(45) [6]=> string(3) "for" [7]=> int(1994) }
  1. unset() Function to Remove Empty Elements.

The Another way is to remove empty elements from array is using empty() function along with the unset() function. The empty() function is used to check whether the element is empty is not.

<?php
// Declaring the array
$array = array("bishrul", 27, '', null, "developer", 45,"for", 1994, false,);

// Loop to find empty elements
foreach($array as $key => $value)
  if(empty($value)){
  unset($array[$key]);
}

// Displaying the array
foreach($array as $key => $value)
    echo ($array[$key] . " , ");
?>  
 

Output :

 

bishrul , 27 , developer , 45 , for , 1994 ,

 

 

Removing a specific value using unset() function in PHP.

<?php
// Declaring the array
$array = array("bishrul", 27, '', null, "developer", 45,"for", 1994, false,);

// Loop to find empty elements
foreach($array as $key => $value)
  if($value == "bishrul"){
  unset($array[$key]);
}

// Displaying the array
foreach($array as $key => $value)
    echo ($array[$key] . " , ");
?>  
 

Output :

27 , , , developer , 45 , for , 1994 , ,

Hope this tutorial helped you! Feel free to drop your opinion at the comment section.

Share:

Related Post

Installing PHP on Windows

PHP is known to be one of the most commonly used server-side programming language on the web. It provides an easy to master with a simple learning curve. It has close ties with the MySQL database.

  • 3 years ago

Convert PHP Array To JSON : Examples

This tutorial focuses on converting PHP Array to JSON. It is used to read data from the server and display it to the Web. JSON is a text format , and We can convert any JS Object into a JSON format.

  • 5 years ago

Simple PHP Page Router

This is a simple yet basic PHP routing application created to direct all request to index.php and route the files to it’s relevant paths.

  • 5 years ago

Simple PHP MySQL CRUD Application

PHP is widely used by many developers today and it’s one of the easiest language to learn. This tutorial will help you to learn the basic CRUD (Create, Read, Update, Delete) operations.

  • 5 years ago

PHP Array Functions and their usage with examples

This tutorial is created to give an abstract idea about commonly used PHP array function with examples. Through this tutorial you’ll be able to know, how to use array functions to make code more short

  • 4 years ago