0

I have a array which i have extracted from html using DOM. Now as the below array shows there are many empty data which i dont want. So wenever i try to remove the empty values from the array its not removed.

Array ( [0] => [1] => Outpost Congratulations13 [2] => [3] => [4] => [5] => [6] =>
 [7] => Yard will reflect the type of work that they do and the strength and variety of their membership, from recent graduates to emerging and mid-career artists. 
[8] => [9] => [10] => [11] => Gallery  Closed Good Friday, open Bank Holiday Monday. Admission Free 
[12] => [13] => K  Yard, Castle Street 
[14] => [15] => Friday 1 Mar 3 [16] => [17] => [18] => [19] => www.somesite.co.uk 
[20] => [21] => [22] => [23] => Map [24] => [25] => Contact the Organiser Tell a Friend about this Event [26] => [27] => Plan Your Journey [28] => [29] => [30] => )

What all i have tried:-

  1. array_filter : It did nt work.
  2. Many functions to check whether the value is empty or not still did not work.
  3. I tried to use strlen to find the length of empty strings , But it shows 22, 2 30 as the lenght.
  4. I use str_replace to replace spaces with ntg still nt working and stlen is showing 22, 28,etc for empty values.
  5. I have used trim bt no use...

Can anyone help me out as to why the strlen of a data is 22 or more. And how to remove these type of elements from array???

12
  • 1
    YUp i tried , still the same result Commented Mar 12, 2013 at 13:16
  • 1
    Could you make the code available in codepad? Commented Mar 12, 2013 at 13:17
  • 1
    Could you post the array as PHP code so we can copy and paste, please? Commented Mar 12, 2013 at 13:17
  • 2
    are you sure they are empty? do a var_dump(); instead - If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. Commented Mar 12, 2013 at 13:17
  • 2
    An empty string with length of 22 is highly suspicious; could you echo bin2hex($arr[0])? Commented Mar 12, 2013 at 13:22

3 Answers 3

3

since the data has empty strings (22 spaces etc) we need to trim them

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

11 Comments

Nope i tried it long back did nt work..........i already mentioned above, trim, str_replace....none worked
+1. Judging from the hex output, this is most likely the right answer :)
That'll remove string(1) "0" as well, though.
Thats why I +1'd your answer
@PavanK You can apply bin2hex() on those strings as well to find out what characters are inside.
|
3

This should do what you need:

$array = array(
  'Hello',
  '',
  0,
  NULL,
  FALSE,
  '0',
  '    ',
);

$new_array = array_filter($array, function ($value)
{
    return strlen(trim($value));
}
);

This will give:

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

The problem with using array_filter($array) or array_filter($array, 'trim') is that string/integer 0 will also be removed, which presumably isn't what you want?

Edit:

If you're using PHP < 5.3, use the following:

function trim_array ($value)
{
    return strlen(trim($value));
}

$new_array = array_filter($array, 'trim_array');

6 Comments

+1. This is more explicit in which string property should be evaluated :)
@MichaelRushton how can we use function within the array_filter ?? I am getting syntax error
Sorry, had a partially removed print_r in there before. Fixed.
Its shoeing language feature nt compatible with php....can we use the function outside >?
What version of PHP do you have?
|
0
function array_remove_empty($arr){
    $narr = array();
    while(list($key, $val) = each($arr)){
        if (is_array($val)){
            $val = array_remove_empty($val);
            // does the result array contain anything?
            if (count($val)!=0){
                // yes :)
                $narr[$key] = $val;
            }
        }
        else {
            if (trim($val) != ""){
                $narr[$key] = $val;
            }
        }
    }
    unset($arr);
    return $narr;
}

array_remove_empty(array(1,2,3, '', array(), 4)) => returns array(1,2,3,4)

2 Comments

It's kinda long, but go through it.
Not sure whether recursion was really applicable here ;-)

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.