1

I have tried using php explode() function but its taking the empty values in to the array.

here is my code

var_dump(explode(',', 'abc,efg,,hij,'));
array(0) {
  [0]=>"abc"
  [1]=> "efg"
  [2]=> ""
  [3]=>"hij"
  [4]=> ""
}

What i expect is

array(0) {
  [0]=>"abc"
  [1]=> "efg"
  [2]=> "hij"

}
John Conde
  • 217,595
  • 99
  • 455
  • 496
Abid
  • 344
  • 1
  • 4
  • 21
  • use `array_filter($array)` – Shahroze Nawaz Aug 01 '17 at 11:25
  • You can delete empty values: [https://stackoverflow.com/questions/34781807/how-to-remove-empty-and-null-values-from-an-array-in-php](https://stackoverflow.com/questions/34781807/how-to-remove-empty-and-null-values-from-an-array-in-php) – Michal Picpauer Aug 01 '17 at 11:27
  • Technically, he first needs to convert the comma separated string into an array using explode() then do the array_filter(); The correct answer is below – giolliano sulit Aug 01 '17 at 11:28
  • possible duplicate of :- https://stackoverflow.com/questions/3654295/remove-empty-array-elements?rq=1 – kunal Aug 02 '17 at 12:19

5 Answers5

5

array_filter() will remove empty elements from an array:

$results = array_filter(explode(',', 'abc,efg,,hij,'));

Demo

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • array_filter is filter the data only how you can set the keys again 0,1,2 etc so after you have to use array_values also. So i think this is not the accepted answer – kunal Aug 01 '17 at 11:40
  • 1
    Well, he didn't specify that he needed the keys and from what it looks like he's trying to do, he won't need the keys. The question was how can he **remove empty values** and return an array. But yes if he wants to get the reset the keys in numerical order, than array_values() will need to be run. – giolliano sulit Aug 01 '17 at 11:45
  • 1
    @giollianosulit you can see the question again he says that :- What i expect is ...,. after that see that array – kunal Aug 01 '17 at 11:47
  • 1
    Well yep, I guess you're right, I'm just going by him not explicitly saying it, that he just assumed that's how the array would look once you remove values. – giolliano sulit Aug 01 '17 at 11:50
  • The cruft of their question was how to get an array of those string values without blank elements. If they want to re-order the keys they didn't say so. But, if they do, I think it's clear that they know how to do that. – John Conde Aug 01 '17 at 11:55
  • @JohnConde why you delete my comment – kunal Aug 02 '17 at 04:03
  • @JohnConde if i suggest you good thing before answering any question you must follow these protocols. this is not the right way to give uncomplete answer – kunal Aug 02 '17 at 04:04
2

This is Simple to remove the empty values just use Filter method and they will be eliminated.

<?php
$array = array("apple", "", 2, null, -5, "orange", 10, false, "");

// Filtering the array
$result = array_filter($array);                 
var_dump($result);
?>
Shahroze Nawaz
  • 589
  • 5
  • 9
  • You should explain how this solves their problem. It would also be a better answer if your answer had an example using their code. – John Conde Aug 01 '17 at 11:29
1

You can achieve this by use array_filter() php function check here http://php.net/manual/en/function.array-filter.php.
It will remove empty values from the array.

Try like this

$array=explode(',', 'abc,efg,,hij,');   
array_filter($array)

It will give o/p as

array(0) {
  [0]=>"abc"
  [1]=> "efg"
  [2]=> "hij"
}
Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51
  • 1
    Do or do not. There is no try. You should explain *how* this solves their problem. Does `array_filter()` use magic? Also, your code is not correct as it currently is written. – John Conde Aug 01 '17 at 11:27
1

You can use this:-

$results = array_filter(explode(',', 'abc,efg,,hij,'));
$results = array_values($results);
print_r($results);

Hope it helps!

kunal
  • 4,122
  • 12
  • 40
  • 75
1

read the manual

array_filter() without callback remove the null and empty from array

 // Filtering the array
   $result = array_filter($array);                 
    var_dump($result);
Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • You should explain how this solves their problem. It would also be a better answer if your answer had an example using their code. – John Conde Aug 01 '17 at 11:29