foreach skip a value

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

  1. #1
    Hi all, I need a help, if I have a multiple values in a array, how can I skip a value ?
    E.g
    $dp = array('first','secode','third','fourth','fifth,'sixth');
    foreach($dp as $php)
    {
    echo $php;
    if($php = 'third') skip and next;
    }
    PHP:

     
    Solved! View solution.
    vOlLvEriNe, Feb 21, 2014 IP
  2. #2
    Uhm, from your code you want to skip the fourth one, right?

    If so, put what you do in a IF statement with a NOT.

    foreach ($dp as $php) {
    	if ($php != 'fourth') echo $php;
    }
    Code (markup):
    Will echo 'firstsecodethirdfifthsixth'

    Another approach would be to run your if statement BEFORE you do anything, and use "continue".

    foreach ($dp as $php) {
    	if ($php == 'fourth') continue;
    	echo $php;
    }
    Code (markup):
    The trick being to 'continue' BEFORE the operation that outputs, instead of after.

    Though you might want to check your string (you missed a single quote), and you used assignment (=) instead of compare (==)
     
    deathshadow, Feb 21, 2014 IP
  3. vOlLvEriNe

    vOlLvEriNe Member

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    Thanks dear
     
    vOlLvEriNe, Feb 22, 2014 IP
  4. Usama Aziz

    Usama Aziz Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    123
    #4
    use "If(condition_is_true) continue;"
    continue function will skip the value
     
    Usama Aziz, Mar 12, 2014 IP
  5. vOlLvEriNe

    vOlLvEriNe Member

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    vOlLvEriNe, Mar 15, 2014 IP