SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with arrays

    Hi there,

    I've got an unusual problem here, when checking if the imploded array has any value (eg. say if an array had three or four zero string values) I want to recognize that fact.

    However, php for some reason does not do what is expected from the line:

    "if ($arrayimploded != '' || (strlen(trim($arrayimploded)) > 0) || $arrayimploded != null) {"

    ?

    What am I doing wrong?

    Thanks in advance

    Code:
    if (is_array($itemtocheck) && sizeof($itemtocheck) > 0) {
    
    echo '   its an array   ';
    
    $arrayimploded = implode($itemtocheck);
    
    echo ' this is imploded :'.$arrayimploded . ':upto:' . sizeof($itemtocheck) . ':variables inside this array';
    
    if ($arrayimploded != '' || (strlen(trim($arrayimploded)) > 0) || $arrayimploded != null) {
    echo 'there's definitely something in this array';
    	} else {
    echo 'this array seems empty';
    
      }
    
    } else {
    echo 'it wasn't an array being checked';
    	}

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "zero string values"
    Do you mean empty string values? like "" or string 0 like "0"

    I'm guessing empty string. And concatenating multiple empty strings, will always result in an empty string. You could use a delimiter for implode() so the resulting string contains something when there is more than one element in the array.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    "zero string values"
    Do you mean empty string values? like "" or string 0 like "0"

    I'm guessing empty string. And concatenating multiple empty strings, will always result in an empty string. You could use a delimiter for implode() so the resulting string contains something when there is more than one element in the array.
    Thanks for the reply. With regards to the string, it is the empty string I mean as in "". I've used the delimiter as another space as well as no delimiter, but both give similar results (i.e. it will not agree that the array values are "empty"). Unsetting the array if it the conditions evaluate to true in a foreach loop gets the job done but leads to a lot more code (and messy at that) so I want to exhaust this avenue first. I'm assuming I'm doing something wrong (the alternative is that php is simply crap!).

    Thanks again.

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by teamwhatever View Post
    Thanks for the reply. With regards to the string, it is the empty string I mean as in "". I've used the delimiter as another space as well as no delimiter, but both give similar results (i.e. it will not agree that the array values are "empty"). Unsetting the array if it the conditions evaluate to true in a foreach loop gets the job done but leads to a lot more code (and messy at that) so I want to exhaust this avenue first. I'm assuming I'm doing something wrong (the alternative is that php is simply crap!).

    Thanks again.
    PHP isn't crap. You're just doing something wrong. Can you post the array you using? Also please take a look at the definition of the implode. As you are using it you haven't actually supplied an array parameter. You have only supplied the glue. The proper usage is as follows.

    PHP Code:
    <?php

    // http://us2.php.net/implode
    $array = array('lastname''email''phone');
    $comma_separated implode(","$array);

    echo 
    $comma_separated// lastname,email,phone

    ?>
    Also, when you're posting php code please use the PHP enclosures rather than the CODE enclosures so that vBulletin can apply syntax highlights.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post some (working) code, including a sample array.

    Post your expected output.

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by imaginethis View Post
    PHP Code:
    <?php

    // http://us2.php.net/implode
    $array = array('lastname''email''phone');
    $comma_separated implode(","$array);

    echo 
    $comma_separated// lastname,email,phone

    ?>
    Hi all, thanks for the replies.

    According to php.net manual, the glue is optional as of php 4.3? Eitherway, I've tried with the glue and the results are the same.

    PHP Code:
    <?php
    //this is the array being used
    $itemtocheck = array('','');

    if (
    is_array($itemtocheck) && sizeof($itemtocheck) > 0) {

    echo 
    'its certainly an array and has values  ';

    //amended to include glue
    //$arrayimploded = implode($itemtocheck);
    $arrayimploded implode(' ',$itemtocheck);

    echo 
    ' this is imploded :'.$arrayimploded ':upto:' sizeof($itemtocheck) . ':variables inside this array -- this is it imploded and trimmed :' trim($arrayimploded) . ':';

    if (
    $arrayimploded != '' || (strlen(trim($arrayimploded)) > 0) || $arrayimploded != null) {
    echo 
    'there's definitely something in this array';
        } else {
    echo '
    this array seems empty';

      }

    } else {
    echo '
    it wasn't an array being checked';
        }

    ?>
    The result I get is as follows:
    Code:
    its certainly an array and has values
    
     this is imploded : :upto:2:variables inside this array  -- this is it imploded and trimmed ::
    
    there's definitely something in this array
    Which is patently wrong, as that final line shouldn't even be executed since the

    PHP Code:
    if ($arrayimploded != '' || (strlen(trim($arrayimploded)) > 0) || $arrayimploded != null) { 
    should mean that the conditions are not true for it to be executed? I should receive this array seems empty?

    I'm sure I must be doing something wrong, I just for the life of me can't see what.

    Thanks again for the replies, much appreciated!

  7. #7
    SitePoint Addict Beaumont's Avatar
    Join Date
    Mar 2005
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really don't understand what you're trying to do, but in your latest example you are joining a 2-element array with space (" ") as the glue, which means $arrayimploded is going to end up as the string " " (a single space character) and therefore ( $arrayimploded != '' ) will evaluate to TRUE and 'there's definitely something in this array' will be echoed.

    Before that IF statement insert a var_dump( $arrayimploded ); to see that.

    What are you actually trying to accomplish?

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Please run the following test script (much like your own):
    PHP Code:
    <?php

    $itemtocheck 
    = array('''');
    var_dump($itemtocheckimplode($itemtocheck));

    if (
    is_array($itemtocheck) && sizeof($itemtocheck) > 0) {
        
    $arrayimploded implode($itemtocheck);
        if (
    $arrayimploded != '' || (strlen(trim($arrayimploded)) > 0) || $arrayimploded != NULL) {
            echo 
    "There's definitely something in this array";
        } else {
            echo 
    "This array seems empty";
        }
    } else {
        echo 
    "It wasn't an array being checked";
    }

    ?>
    The output should be:
    Code:
    array(2) {
      [0]=>
      string(0) ""
      [1]=>
      string(0) ""
    }
    string(0) ""
    This array seems empty
    Let us know if your results are different to mine.

    Off Topic:


    The array_filter function might come in handy if you're simply wanting to remove any "empty" values from the provided array (those values that evaluate to boolean FALSE if no callback is provided). If you need a specific check on the values (only remove array items that contain the word apple, to give a silly example) then you can provide a callback which can examine each value and return TRUE/FALSE stating whether to keep the value or not respectively.
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your test doesn't make sense:

    PHP Code:
    if ($arrayimploded != '' || (strlen(trim($arrayimploded)) > 0) || $arrayimploded != null) { 
    is literally "Return true if the string is not empty OR the trimmed string has more than 0 characters OR the string is not null"

    The first two tests are essentially testing the same thing. But the 3rd test would be true even if the string had 0 characters, thus this statement would ALWAYS return true since we know $arrayimploded cannot be null since you set it explicitly above and the implode function wouldn't return null.

    If all you want to do is test if an array contains empty values, then something like this would work:
    PHP Code:
    function arrayHasEmptyValues($array$trim true)
    {
      foreach((array) 
    $array as $v)
      {
        if (
    is_null($v)) return true;
        if (
    $trim$v trim($v);
        if (
    strlen($v) == 0) return true;
      }
      return 
    false;

    It will return true if any empty values are present, or false otherwise. Note that it returns false if the array itself is empty.
    Last edited by xangelusx; Sep 29, 2009 at 08:19. Reason: optimizing function
    Chris Bloom
    Web Application Developer

  10. #10
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PS: if for some reason you can only check the value AFTER imploding, then just test that strlen(trim($arrayimploded)) > 0, but keep in mind what Beaumont warned about: implode(':', array('','')) still equals ':'!
    Chris Bloom
    Web Application Developer

  11. #11
    SitePoint Addict Beaumont's Avatar
    Join Date
    Mar 2005
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xangelusx View Post
    But the 3rd test would be true even if the string had 0 characters, thus this statement would ALWAYS return true since we know $arrayimploded cannot be null since you set it explicitly above and the implode function wouldn't return null.
    That's incorrect. If $arrayimploded is the empty string, then the third comparison will evaluate to FALSE. See the Loose comparisons with == table and note that != is the negated counterpart of ==.

    I agree that the tests that the OP is performing don't seem to make much sense. If the OP explains what they are trying to accomplish, it would be possible to advise them.

  12. #12
    SitePoint Zealot
    Join Date
    May 2003
    Location
    Sarasota, FL
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Beaumont View Post
    DOH! Thanks for correcting me
    Chris Bloom
    Web Application Developer

  13. #13
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Beaumont View Post
    I agree that the tests that the OP is performing don't seem to make much sense. If the OP explains what they are trying to accomplish, it would be possible to advise them.
    That's the crux of it - understanding the end goal may allow us to offer a more-suitable solution.
    Ian Anderson
    www.siteguru.co.uk

  14. #14
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi all,

    Thanks for the all the replies and apologies for the delay in replying. I've resolved the problem using a different method.

    What I'm trying to achieve is to check that a variable is set (i.e. not null), not space only string without any other characters, not an empty variable, and to also perform that test on arrays (value), which is the bit I've included in this thread.

    The test itself is correct (as already noted) as it will check for an empty string, or trim the variable of any start space and end space and check if that returns greater than 0 characters and finally if it's a null and go true accordingly.

    You'll note that the implode glue was added only after imaginethis incorrectly advised that the usage of implode was incorrect. The original post shows it without the glue (and that was the intended method).

    @Salathe thanks for the example, it appears to essentially differ in the var_dump line only, but I'll test it and let you know as a matter of interest. I want to know if it does make a difference

    Big thank you again all for your replies and suggestions.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •