0

No doubt it has been discussed few million times here and yes once again I am posting something related with it.

I have the following code.

$address = "#&#&#&";
$addr = explode("#&",$address);

it returns

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

Now the values are empty.So I am doing a check.

If(!empty($addr))
{
echo 'If print then something wrong';
}

And it still prints that line.Not frustrated yet but will be soon

1
  • preg_split() with the PREG_SPLIT_NO_EMPTY flag may be of interest to you. It splits strings based on a regular expression and allows you to discard empty pieces with the mentioned option. Commented Aug 2, 2014 at 23:33

6 Answers 6

6

Run array_filter() to remove empty entries, then check if the array itself is empty()

if (empty(array_filter($addr)))

Note that array_filter() without a callback will remove any "falsey" entries, including 0

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

Comments

2

To check if the array contains no values you could loop the array and unset the no values and then check if the array is empty like the following.

$addresses = "#&#&#&";
$addresses = explode("#&", $addresses);

foreach ( $addresses as $key => $val ) {
   if ( empty($val) ) {
      unset($addresses[$key]);
   }
}

if ( empty($addresses) ) {
   echo 'there are no addresses';
}
else {
   // do something
}

1 Comment

Thanks Jake.I will prefer to check before going down through the loop.
1

This is not an empty array, empty array has no elements, your have four elements, each being an empty string, this is why empty is returning false.

You have to iterate over elements and test whether they are all empty, like

$empty=True;
foreach ($addr as $el){ 
 if ($el){
  $empty=False;
  break;
 }
}

if (!$empty) echo "Has values"; else echo "Empty";

1 Comment

Wouldn't work. Since you're not checking if it is empty.
1

You are checking if the array contains elements in it. What you want to do is check if the elements in the array are empty strings.

foreach ($addr as $item) {
    if (empty($item)) {
        echo 'Empty string.';
    } else {
        echo 'Value is: '.item;
    }
}

Comments

0

The array is not empty, it has 4 items. Here's a possible solution:

$hasValues = false;

foreach($addr as $v){
    if($v){
        $hasValues = true;
        break;
    }
}

if(!$hasValues){
    echo 'no values';
}

Comments

0

Thinking outside of the box, your task appears to want to validate that no values have been added beyond the delimiting character sequences. For this reason, you don't actually need to produce an array, then check that array. Instead, just check if the input string is fully comprised of your delimiting sequences.

Code: (Demo)

$address = "#&#&#&";
$delimiter = '#&';
if (preg_match('~^(?:' . preg_quote($delimiter) . ')+$~', $address)) {
    echo 'Text comprised solely of one or more delimiting substrings';
}

In truth the preg_quote() call is not actually needed, I just thought it would make the answer more robust for general use. The pattern can simply be /^(?:#&)+$/.

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.