Posted inphp

php array remove empty – How to remove empty values from an array in PHP?

php array remove empty Use the PHP array_filter() function. The unset() function removes the value stored in a variable.

php array remove empty

We can use it to remove the empty elements from an array. You can use the PHP array_filter() function remove empty array elements or values from an array in PHP.

php array remove empty
Remove empty array elements

print_r(array_filter($products_arr));

You can use array_filter to remove empty elements:

$emptyRemoved = array_filter($products_arr);

Use unset() Function, Use array_diff() Function, Use array_filter() Function to Remove the Empty Array Elements in PHP
Example #1 array_filter() example


Result

Array
(
    [0] => Laravel
    [1] => Angularjs
    [2] => Vuejs
    [4] => ReactJS
)

Example #2 array_filter() example with reindex array elements


Result

Array
(
    [0] => Laravel
    [1] => Angularjs
    [2] => Vuejs
    [3] => ReactJS
)

Removing Empty Array Elements by Looping Over the Array

$products_arr = $products_arr = array('Pakainfo', '', false, null, '0', 'false');

/* This Method won't Work */
foreach($products_arr as $elem) {
    if($elem === '') {
        unset($elem);
    }
}

var_dump($products_arr);
/* Output —
array(6) {
  [0]=>
  string(4) "Pakainfo"
  [1]=>
  string(0) ""
  [2]=>
  bool(false)
  [3]=>
  NULL
  [4]=>
  string(1) "0"
  [5]=>
  string(5) "false"
} */


/* This Method will Work */
foreach($products_arr as $key => $value) {
    if($value === '') {
        unset($products_arr[$key]);
    }
}

var_dump($products_arr);
/* Output —
array(5) {
  [0]=>
  string(4) "Pakainfo"
  [2]=>
  bool(false)
  [3]=>
  NULL
  [4]=>
  string(1) "0"
  [5]=>
  string(5) "false"
} */

remove item from array php

Deleting an element from an array in PHP
Using PHP unset() Function

 "amit", "b" => "bharti", "c" => "chandni");
  unset($products_arr["b"]);
?>

Result

array("a" => "amit", "c" => "chandni")

Using PHP array_splice() Function


Result

Array
(
    [0] => 1
    [1] => 2
    [2] => 5
)

don’t Miss : Remove Empty Values From Array In PHP

remove character from string

How to Remove Special Characters from String In PHP?

The following script will remove some special characters from a string using str_replace() function

Text before remove:  
".$products_arr; //Call the function $replacestr = rm_special_char($products_arr); //Define the function to remove the spacial character function rm_special_char($str) { //Remove "#","'" and ";" using str_replace() function $result = str_replace( array("#", "'", ";"), '', $str); //The output after remove echo "
Text after remove:
".$result; } ?>

php remove first character

Remove First 2, 3, 4, 5, 10, etc, Character From String PHP
using substr function

 $products_arr = "Welcome To Pakainfo.com!";
 echo "Given string: " . $products_arr . "\n";
 echo "Updated string: " . substr($products_arr, 1) . "\n"; 

php remove array element by key

How to delete an array element based on key in PHP?

unset($websites[1]);

php delete element by value

$websites = array("pakainfo","w3diy","infinityknow");

//delete element in array by value "w3diy"
if (($key = array_search("w3diy", $websites)) !== false) {
    unset($websites[$key]);
}

Remove empty array elements

To remove empty values from an array in PHP, you can use the array_filter() function, which filters the elements of an array based on a callback function. In this case, we can use the empty() function as the callback to remove the empty values. Here’s an example:

$my_array = array("apple", "", "banana", "", "cherry", " ");

$my_array = array_filter($my_array, 'strlen');

print_r($my_array);

In this example, we have an array with some empty values. To remove the empty values, we pass the array to the array_filter() function, along with the strlen function as the callback. The strlen function returns the length of a string, so if a value is not empty, its length will be greater than zero, and it will be kept in the array. If a value is empty (i.e., its length is zero), it will be removed from the array.

The print_r() function is used to print the resulting array to the screen, which will show only the non-empty values.

Alternatively, you can also use the array_diff() function to remove empty values. Here’s an example:

$my_array = array("apple", "", "banana", "", "cherry", " ");

$my_array = array_diff($my_array, array(''));

print_r($my_array);

In this example, we pass the original array to the array_diff() function, along with an array containing only an empty string (”) as the second argument. The array_diff() function returns an array containing only the elements that are present in the first array but not in the second array, which in this case will be the non-empty values.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype