foreach (loop) help

Discussion in 'PHP' started by vOlLvEriNe, Feb 9, 2014.

  1. #1
    I have 2 conditions,
    first one is
    foreach ($list as $p)
    PHP:
    and secode one is
    foreach ($list2 as $p2)
    PHP:
    now i want to make like this,
    foreach ($p+$p2 as $total)
    PHP:
    how can I make this ?
    please help
     
    Solved! View solution.
    Last edited: Feb 9, 2014
    vOlLvEriNe, Feb 9, 2014 IP
  2. alex_e

    alex_e Well-Known Member

    Messages:
    1,011
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    195
    Digital Goods:
    3
    #2
    I dont know foreach, but for IF, do something like this:

    <?php
    $p = 1;
    $p2 = 2;
    $total = 3;
    
    if($p+p2 == $total){
    do something
    }
    
    else {
    
    Do something else
    
    }
    
    ?>
    Code (markup):
     
    alex_e, Feb 9, 2014 IP
  3. vOlLvEriNe

    vOlLvEriNe Member

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    No no, I ask a differently change situation :(
     
    vOlLvEriNe, Feb 9, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    
    <?php
    
    $list = [1,2,3,4,5,6];
    $list2 = [1,2,3,4,5,6];
    
    $count = count($list);
    for ($i = 0; $i < $count; $i++) {
       echo $total = $list[$i] + $list2[$i];
    }
    
    ?>
    
    PHP:
    Assumes both arrays are of the same length
     
    PoPSiCLe, Feb 9, 2014 IP
  5. vOlLvEriNe

    vOlLvEriNe Member

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    Dear see this,
    $first = array('red','orange','yellow','green','lime','maroon','white','black');
    $second = array('first','second','third','4th','5th','6th','7th','8th');
    
    foreach ($first AND $second as $third){
    echo $third;
    }
    
    PHP:
    I Want result like this

    red,orange,yellow,green,lime,maroon,white,black,first,second,third,4th,5th,6th,7th,8th
    Code (markup):

    How can I combine this ?
     
    vOlLvEriNe, Feb 10, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    You just want to output both arrays after eachother? That doesn't involve any kind of plus signs...
    
    echo $list = join(',',array_merge($list,$list2));
    
    Code (markup):
     
    PoPSiCLe, Feb 10, 2014 IP
  7. vOlLvEriNe

    vOlLvEriNe Member

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    Bro one more question,
    If have this
    $first = array('red','orange','yellow','green','lime','maroon','white','black');
    foreach ($first as $list)
    $second = array('first','second','third','4th','5th','6th','7th','8th');
    foreach ($second as $list2)
    
    PHP:
    Now what should I do ?
    foreach ($list AND $list2 as $last) 
    PHP:
     
    vOlLvEriNe, Feb 10, 2014 IP
  8. #8
    uhm... you just do
    
    echo $last = join(',',array_merge($first,$seond));
    
    PHP:
    No need for a loop, or reassigning the arrays.
     
    PoPSiCLe, Feb 10, 2014 IP