2

I have this array of string:

array(59) {
    [0]=> string(25) "shadowcores.twifysoft.net"
    [1]=> string(11) " "
}

Obviously there are 57 elements more. I need to know how can I remove this:

string(11) " "

There is no string to display but the string itself its not empty. How can I check for these types of values before adding them into my array?

5
  • Protip: Add header('Content-Type: text/plain'); to the top of your script. Commented Dec 4, 2013 at 16:39
  • if ($arr[$i] == ' ') { unset($arr[$i]); }? Commented Dec 4, 2013 at 16:40
  • already tried that the thing is the string seems to have hidden characters or something Commented Dec 4, 2013 at 16:41
  • Perhaps there is some unicode in there? Apparently something thinks there are 11 characters (or bytes). Commented Dec 4, 2013 at 16:44
  • The trim array_filter seems to be working well Commented Dec 4, 2013 at 16:45

6 Answers 6

8

Use the following:

$array = array_filter($yourArray, 'trim');
Sign up to request clarification or add additional context in comments.

6 Comments

@AmalMurali array_filter($yourArray, 'trim') will remove that ' ' element from the array while array_map('trim',$array) trim it to '' and still keep it in the array.
@PaulLo: Yeah, but however, the callback in your code is redundant, as it just repeats the default behavior.
@AmalMurali Yeah, if incorrectly use array_filter($yourArray, trim()) as my original post, it actually have the same effect as array_map('trim', $array)
@PaulLo: array_filter($yourArray, trim()) would never work, because trim() expects at least one parameter and you'll be passing none. The correct way to pass callback is: array_filter($array, 'callback');. My point was this: array_filter() by default filters out empty array elements, so the trim callback is redundant and not actually necessary.
@AmalMurali Oh yeah, as you said, I found array_filter($yourArray, trim()) would never work. But looks like we cannot just use array_filter($array) in this case since that ' ' blank string is not evaluated as an empty element.
|
3

Loop through the array and check to see if the string is empty after trimming whitespace

if (empty(trim($string))) {
     // unset from array
}

Comments

2

Trim them beforehand or use array_filter to get rid of the just-whitespace elements

Comments

2

There are multiple ways to skin this cat.

Personally, I'd go for running the variable through trim() and then check to see if this value is empty() or has a strlen() == 0. If this is the case, you can skip adding it to the array.

1 Comment

He explicitly tell us that string(11) " ". Thus the string has 11 characters ... ?
2

To remove empty array values, you can use array_filter().

From the manual page:

If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed.

So, simply call:

$array = array_filter($array);

And if you want to remove whitespace from the beginning and end of all array values, you can use array_map():

$array = array_map('trim', $array);

Comments

1

There is several options. to remove theses values from the array you can use that technique:

foreach ($array as $k => $v)
{
  if (trim($v) == '')
    unset($array[$k]);
}

Comments

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.