SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Plugging in Array

  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Plugging in Array

    The script below displays "North America," where $Name is an array that includes the value "North_America."

    PHP Code:
    $val array_search('North_America'$Name);
    if (
    $val>-1)
    echo 
    'North America';
    else
    echo 
    "Value isn't present."
    Can I replace 'North_America' with an array that includes all the continents? If so, how? I tried the script below, but it isn't working. Thanks.

    PHP Code:
    $Continents = array("North_America""South_America""Africa");
    $query "SELECT * FROM gzdistpractice";
     
    $result mysql_query($query);
     while(
    $row mysql_fetch_array($result)) {
          
    $Name[] = $row['Name'];
    echo(
    $row['Name']);
    print_r($Name);
    }

    $val array_search($Continents$Name);
    if (
    $val>-1)
    echo 
    'Continent';
    else
    echo 
    "Value isn't present."

  2. #2
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of
    PHP Code:
    $val array_search($Continents$Name);
    if (
    $val>-1)
    echo 
    'Continent';
    else
    echo 
    "Value isn't present."
    How about
    PHP Code:
    foreach($Continents as $continent)
         if (
    in_array($continent$Name)) 
              echo 
    "$continent present"

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Scheisskopf
    Instead of
    PHP Code:
    $val array_search($Continents$Name);
    if (
    $val>-1)
    echo 
    'Continent';
    else
    echo 
    "Value isn't present."
    How about
    PHP Code:
    foreach($Continents as $continent)
         if (
    in_array($continent$Name)) 
              echo 
    "$continent present"
    Wow, that's a bonus script. It works, plus it illustrates that the in_array function works on my computer. It appears that in_array doesn't work with dynamically generated arrays, apparently because of some sort of bug in PHP. I thought it wouldn't work at all, so this is a nice surprise. Thanks.

  4. #4
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite
    It appears that in_array doesn't work with dynamically generated arrays, apparently because of some sort of bug in PHP.
    Really? I find it kind of hard to believe..

  5. #5
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite
    Wow, that's a bonus script. It works, plus it illustrates that the in_array function works on my computer. It appears that in_array doesn't work with dynamically generated arrays, apparently because of some sort of bug in PHP. I thought it wouldn't work at all, so this is a nice surprise. Thanks.
    Can you give an example of this bug?

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dylan B
    Can you give an example of this bug?
    That's a good question! I spent a couple days wrestling with some array questions, then started another thread to discuss the error. I think the first link below is one of the original discussions, and the second link regards the error message.

    http://www.sitepoint.com/forums/show...=1#post2251964
    http://www.sitepoint.com/forums/show...=1#post2252550

    I finally asked about it on another forum, and someone told me the problem was me:

    "What are you trying to do? Why are you trying to use an array function
    on a string? When you get the $output array, the array element
    $output['Name'] is presumably a string, not an array. The second
    argument to the in_array() function is supposed to be an array. Are you
    simply trying to find out if the Name field contains the word
    "Neotropical"?"

    He suggested I try this script:

    if(preg_match("/Neotropical/",$output['Name']))

    It seems to work. So I'm assuming I got the error message because I did something wrong. It was really confusing because one or two people told me they duplicated my scripts and they worked. In fact, I think I tried several different PHP functions, and none of them worked. But I may simply have been doing everything wrong.

    Also, I found lots of references to this error, including several on Drupal's website.

    So I'm thoroughly confused, but things seem to be working out alright for now. I still want to learn how to use the in_array function, but it looks like it's a little more techincal than what was described to me.

    Thanks for your reply.

  7. #7
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The in_array function is really simple, and it works like this:

    PHP Code:
    <?php
    $arr 
    = array('Hello''Giraffe''Dylan''Geosite'); //make a new array with a few sample values.
    if (in_array('Dylan'$arr)) // The value you want to look for is the first argument, and the array you're looking IN is the second argument.
    {
         echo 
    'DYLAN IS IN THE ARRAY'//Hooray!  Victory!
    }
    ?>
    Dylan

  8. #8
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    Finland
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dylan B
    The in_array function is really simple, and it works like this:

    PHP Code:
    <?php
    $arr 
    = array('Hello''Giraffe''Dylan''Geosite'); //make a new array with a few sample values.
    if (in_array('Dylan'$arr)) // The value you want to look for is the first argument, and the array you're looking IN is the second argument.
    {
         echo 
    'DYLAN IS IN THE ARRAY'//Hooray!  Victory!
    }
    ?>
    Dylan
    Yeah, it's just the order of the arguments that confuses many First comes the needle (the thing you're looking for), then the haystack (the array). I always have to check it from php.net, just in case Might be easier to remember if you think of it this way:
    PHP Code:
    in_array('the value that "goes IN"'$array
    And bool strict as the third, optional argument of course.

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
  •