Forum Moderators: coopster

Message Too Old, No Replies

empty function not working for exploded string

         

optik

7:11 pm on Jun 2, 2009 (gmt 0)

10+ Year Member



$string=",,,";

$string=explode(',',$string);

if(empty($string)){

echo "empty";

}else{

echo "full";

}

This should echo empty but it echo's full, how can I check the array is empty?

mooger35

8:26 pm on Jun 2, 2009 (gmt 0)

10+ Year Member



It isn't empty...

Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)

Depending on what your are looking for there are a couple ways to check. I think it would be a good idea to validate each $string value.

optik

11:22 pm on Jun 2, 2009 (gmt 0)

10+ Year Member



is there not a way to quickly check if no values are assigned to any of the keys?

coopster

11:07 am on Jun 3, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If key retention is not necessary you can filter the array first. I do this quite often to remove empty numeric array indexes: array_filter [php.net]

optik

4:23 pm on Jun 3, 2009 (gmt 0)

10+ Year Member



that will do it, thanks.

siMKin

8:40 pm on Jun 5, 2009 (gmt 0)

10+ Year Member



you could even use array_sum in this case

<?php
$str = ",,,,";
echo array_sum(explode(",",$str));
?>

will echo 0