SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Lytham
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach loop in if statement

    is there any way i can print out an array into an if statement.

    e.g.

    I have an array:

    Code:
    $array = array('1' => 'test1', '2' => 'test2', '3' => 'test3');
    and i would like to do a foreach($array as $key => $value) into an if statement so it looked like:

    Code:
    if($item[1] == 'test1' || $item[2] == 'test2' || $item[3] == 'test3') {
           //Do something
    }

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Please note that the first index is 0, not 1. So $array[0] is 'test1' and so on.
    PHP Code:
    foreach ($array as $key => $value) {
      if (
    $value === 'test' . ($key 1)) {
        
    // do something
        // if something gets done, you might want to stop the loop. If so, you need to add a break:
        //break;
      
    }


  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Yes and it's not far off what you have....
    PHP Code:
    foreach($parts as $key=>$value) {
        if((
    $value == 'test1') || ($value == 'test2')) {
            
    // do something
        
    }

    Depending on what you want to do
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Lytham
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't think either of those will help

    basically if I have an array containing

    Code:
    $details =array(0 => array('desk' => 'wood', 'computer => 'mac'),
                         1 =>array('desk' => 'steel', 'computer => 'mac'))
    I want to be able to search that array by passing another array to a function

    eg

    Code:
    $search = array('desk'=>'wood', 'computer'=>'mac');
    
    function search($search) 
    {
         for($i=0; $<count($details); $i++) {
               /*This is where I would need to print the $order array as foreach ($search as $key to value)*/
                if($details[$i]['desk'] == 'wood &&  $details[$i]['computer'] == 'mac') {
                    //Do something
                }
         }
    }

    if

    Code:
    $search = array('desk'=>'wood');
    then it would just be

    Code:
    function search($search) 
    {
         for($i=0; $<count($details); $i++) {
               /*This is where I would need to print the $order array as foreach ($search as $key to value)*/
                if($details[$i]['desk'] == 'wood) {
                    //Do something
                }
         }
    }

  5. #5
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you looking for something like this?
    PHP Code:
    <?php
    function search($search$details) {
        
    $tmp "";
        for (
    $i 0$i <= count($details); $i++) {
            foreach (
    $search as $key => $value) {
                if (
    $details[$i][$key] == $value) {
                    
    $tmp .= "found.<br />\n";
                }
            }
        }
        return 
    $tmp;
    }
    $details = array(=> array("desk" => "wood",
                                
    "computer" => "mac"),
                     
    => array("desk" => "steel",
                                 
    "computer" => "mac"));
    $search = array("computer" => "mac"); // returns 'found.' twice
    $search = array("desk" => "wood"); // returns 'found.' once
    echo search($search$details);
    ?>
    - the lid is off the maple syrup again!

  6. #6
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Lytham
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not quite, for what I intend on doing the if statement would have to change based on the contents of the $search array

    so for

    Code:
    $search = array('desk'=>'wood', 'computer'=>'mac');
    
    if($details[$i]['desk'] == 'wood &&  $details[$i]['computer'] == 'mac') {
    and for

    Code:
    $search = array('desk'=>'wood');
    
    if($details[$i]['desk'] == 'wood') {

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function search($search
    {
         for(
    $i=0; $<count($details); $i++) {
                
    $t=array_intersect_key($details[$i],$search);
                if(
    $search==$t) {
                    
    //Do something
                
    }
         }

    Saul

  8. #8
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Lytham
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent, thankyou


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
  •