#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3

    Question Find an array item from within another array?


    Hi all,

    I've got an array that's built from a query.
    PHP Code:
    $qArray = ("Blue""Red""Green""White""Black""Yellow""Purple"
    I've also got an array that's built from user (form) input.
    PHP Code:
    $uArray = ("2""5""6"
    I'm trying to match up the array (index) number from the User input, to the color in $qArray.

    What I've got now isn't working:

    PHP Code:
    $cntr 0;
    foreach (
    $uArray as $key=>$value) {
        if (
    $key == $cntr) {
            echo 
    $qArray[$cntr]."<br>";
        }
        
    $cntr++;

    Nothing echo's out.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,959
    Rep Power
    9397
    1. You can just use isset().
    2. Assuming the $uArray is like what you posted, you want the values from it not the keys.
    PHP Code:
    foreach ($uArray as $n) {
        if (isset(
    $qArray[$n])) {
            echo 
    $qArray[$n], "<br/>\n";
        }

  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    Why not just...

    PHP Code:
    foreach ( array_keys($array2) as $key ) {
      echo 
    $array1[$key] . "<br />\n";

    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    150
    Rep Power
    3
    Thanks for the reply guys.

    The second suggestion would echo out each of the array items from the query regardless if the user selected that item. Meaning some (most) would be empty.

    This is how I got it to work. I know, not pretty or efficient I'm sure.

    PHP Code:
    foreach ($uArray as $key=>$value) {  //99% this array will be smaller than the next one
        
    foreach ($qArray as $qa) {
            if (
    $key == $qa) {
                echo 
    "Match. Key=".$key." qa=".$qa<br>";
            }
        }

    Thoughts? Is there a more efficient way to do this?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    More than likely. Could you provide more knowledge on the arrays being built? Is the color one a static array, and will always holds those values, while the user selected one will have AT LEAST 1 item?


    If the color array is static, and the numbers in the 2nd array match the position of the first, then this would work just fine, as I believe ManiacDan was aiming for...

    PHP Code:
    <?php
      $qArray 
    = array("Blue""Red""Green""White""Black""Yellow""Purple");
      
    $uArray = array("2""5""6");
      foreach ( 
    $uArray as $key ) { 
        echo 
    $qArray[$key] . "<br />\n"
      }
    ?>
    I may be wrong, but I believe the array_keys() was the issue with his.

    This should echo:
    Green
    Yellow
    Purple

    If you ask why the number 2 echos Green, and not Red, the integer keys start at zero, so Blue will echo on a zero.
    Last edited by Triple_Nothing; March 2nd, 2013 at 08:01 AM.

IMN logo majestic logo threadwatch logo seochat tools logo