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"

}
4

5 Answers 5

5

array_filter() will remove empty elements from an array:

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

Demo

Sign up to request clarification or add additional context in comments.

7 Comments

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
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.
@giollianosulit you can see the question again he says that :- What i expect is ...,. after that see that array
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.
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.
|
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);
?>

1 Comment

You should explain how this solves their problem. It would also be a better answer if your answer had an example using their code.
1

You can use this:-

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

Hope it helps!

3 Comments

You should explain how this solves their problem. Also, why are you using array_values() here?
array_values refresh the array and set the keys like, 0,1,2 and so on simple
@JohnConde see the requirement of user they want result like that
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);

1 Comment

You should explain how this solves their problem. It would also be a better answer if your answer had an example using their code.
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"
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.