0

I am printing out the values using print_r($_POST["prod_sizes"]);

print_r($_POST["prod_sizes"]);

So i get output like this => dsf,,thisnajndk,faklsf,klam,flkamlkd,mklmd,l,,adad

After that i use this code:

$sizes = strip_tags(preg_replace('/[^a-z0-9,]/i', '', $_POST["prod_sizes"]));
$var = explode(',', $sizes);
print_r($var);

I get output like

Array
(
  [0] => dsf
  [1] => 
  [2] => thisnajndk
  [3] => faklsf
  [4] => klam
  [5] => flkamlkd
  [6] => mklmd
  [7] => l
  [8] => 
  [9] => adad
)

As from the above output we can see that there are some blank values in an array. How to remove those blank values?

John Conde
  • 217,595
  • 99
  • 455
  • 496

3 Answers3

5

PHP's built in array_filter() will do this for you:

$sizes = array_filter($sizes);

Keep in mind that any values are equate to false will also be filtered out. So if zero and null is a valid value you will need to write your own callback to filter by.

If you also want to re-index the keys just use array_values() on the result:

$sizes = array_values(array_filter($sizes));
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

Replace your code from this line => $var = explode(',', $sizes); & add this:

$var = array_filter(explode(',', $sizes));
$reindexed = array();

foreach ($var as $row)
    {
        if ($row !== null)
           $reindexed[] = $row;
    }

print_r($reindexed);
exit();

Let's See The Explaination of the code now

1.) This is 1st reference link from where i took the idea to filer but ass you prnt the array you will see that the array indexes are jumbled => Remove empty array elements

$var = array_filter(explode(',', $sizes));
$reindexed = array();

so we create a new variables reindexed as an array to store the reindexed array value

2.) To remove the jumbled array index and reindex the array i took refernce from this link => How to remove null values from an array?

$reindexed = array();

foreach ($var as $row)
    {
        if ($row !== null)
           $reindexed[] = $row;
    }

print_r($reindexed);
Community
  • 1
  • 1
Akshay Shrivastav
  • 1,115
  • 4
  • 17
  • 43
  • 1
    This is an overly complicated way to do something, especially considering PHP already has built in functionality to do this. – John Conde Apr 18 '17 at 14:39
  • @JohnConde even i am new to PHP i just made the code according to my logic and provided, your code is super perfect as it is very clean and easy – Akshay Shrivastav Apr 18 '17 at 14:45
0

Here is the best way:

# Array
$array = array('tomato', '', 'apple', 'melon', 'cherry', '', '', 'banana');

that returns

Array
(
    [0] => tomato
    [1] => 
    [2] => apple
    [3] => melon
    [4] => cherry
    [5] => 
    [6] => 
    [7] => banana
)

by doing this

$array = array_values(array_filter($array));

you get this

Array
(
    [0] => tomato
    [1] => apple
    [2] => melon
    [3] => cherry
    [4] => banana
)

Explanation

array_values() : Returns the values of the input array and indexes numerically.

array_filter() : Filters the elements of an array with a user-defined function (UDF If none is provided, all entries in the input table valued FALSE will be deleted.)

Sandra
  • 1,596
  • 15
  • 22