SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,027
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Bizarre Array Error

    Hello,

    We are having problem with Arrays that is best described with code example:

    the array is: $country_array

    now when we check on the COUNT of items in this array or print them out, the CORRECT count & items are listed as per example:

    echo '<p>country count: ' . count($country_array);
    for ($i = 0; $i < count($country_array); ++$i) {
    echo '<br>' . $country_array[$i];
    }

    printing our: 5
    And:
    United States
    United Kingdom
    Australia
    Canada
    Germany

    However when we do this:

    if (isset($country_array) AND (in_array('United States', $country_array))) {
    echo '<p>United States'; }

    if (isset($country_array) AND (in_array('United Kingdom', $country_array))) {
    echo '<p>United Kingdom'; }

    if (isset($country_array) AND (in_array('Australia', $country_array))) {
    echo 'Australia'; }
    etc.

    it ONLY prints out:
    United States
    and not the other matching items of this array!
    WHAT the HEK is going on?

    ThanX.

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,411
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Could you copy and paste the real code please?

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,826
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Do a var_dump($country_array);, chance are you have whitespace or non-displaying characters throwing off your latter code.
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    works fine for me... are you sure you're not doing anything else between those lines of code?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,907
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    ^^ plus a var_dump of $country_array please. I suspect there are leading or trailing spaces in there ...

    Edit:

    Sod it, cpradio beat me to it
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,027
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I did the var_dump and saw this:

    array(5) { [0]=> string(13) "United States" [1]=> string(14) "United Kingdom" [2]=> string(9) "Australia" [3]=> string(6) "Canada" [4]=> string(7) "Germany" }

    So as you cans see the:
    $country_array = explode(",", $country);

    was leaving a blank space between all items after 1st item!

    So i switched to:
    $country_array = explode(", ", $country);

    and now is working OK!

    Sheeeeeesh

    Anoox search engine volunteer

    www.anoox.com


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
  •