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

<?php
$products_arr = array("Laravel", "Angularjs", "Vuejs", "", "ReactJS", null, 0);
print_r(array_filter($products_arr)); // removing blank, null, false, 0 (zero) values
?>

Result

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

Example #2 array_filter() example with reindex array elements

<?php
$products_arr = array("Laravel", "Angularjs", "Vuejs", "", "ReactJS", null, 0);
print_r(array_values(array_filter($products_arr)));
?>

Free Live Chat for Any Issue

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

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

Result

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

Using PHP array_splice() Function

<?php
  $products_arr = array(1, 2, 3, 4, 5);
  array_splice($products_arr, 2, 2);
  print_r($products_arr)
?>

Result

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

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

Also Read This πŸ‘‰   SKT IT Consultant Wordpress theme By SKT Themes

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

<?php

//Define the main string

$products_arr = "#Welcome to pakainfo's com free download source code;";

//The output before remove

echo "<b>Text before remove: </b> <br/>".$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 "<br/><b>Text after remove: </b> <br/>".$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]);
}