How to skip the 1st key in an array loop?

后端 未结 12 1360
情歌与酒
情歌与酒 2020-12-03 04:29

I have the following code:

if ($_POST[\'submit\'] == \"Next\") {
    foreach($_POST[\'info\'] as $key => $value) {
        echo $value;
    }
}

相关标签:
12条回答
  • 2020-12-03 04:38
    
    
    foreach($_POST['info'] as $key=>$value) {
        if ($key == 0) { //or what ever the first key you're using is
            continue;
        }  else { 
            echo $value;
        }
    }
    
    
    0 讨论(0)
  • 2020-12-03 04:46

    On a array filled with 1000 elements the difference is quite minimal.

    Test:

    <?php
    function slice($a)
    {
        foreach(array_slice($a, 1) as $key)
        {
    
        }
    
        return true;
    }
    
    function skip($a)
    {
        $first = false;
    
        foreach($a as $key)
        {
            if($first)
            {
                $first = false;
                continue;
            }
        }
    
        return true;
    }
    
    $array = array_fill(0, 1000, 'test');
    
    $t1 = time() + microtime(true);
    
    for ($i = 0; $i < 1000; $i++)
    {
        slice($array);
    }
    
    var_dump((time() + microtime(true)) - $t1);
    
    echo '<hr />';
    
    $t2 = time() + microtime(true);
    
    for ($i = 0; $i < 1000; $i++)
    {
        skip($array);
    }
    
    var_dump((time() + microtime(true)) - $t2);
    ?>
    

    Output:

    float(0.23605012893677)

    float(0.24102783203125)

    0 讨论(0)
  • 2020-12-03 04:46

    How about something like this? Read off the first key and value using key() and current(), then array_shift() to dequeue the front element from the array (EDIT: Don't use array_shift(), it renumbers any numerical indices in the array, which you don't always want!).

        <?php
    
        $arr = array(
    
          'one' => "ONE!!",
          'two' => "TWO!!",
          'three' => "TREE",
          4 => "Fourth element",
          99 => "We skipped a few here.."
    
        ) ;
    
        $firstKey = key( $arr ) ;
        $firstVal = current( $arr ) ;
    
        echo( "

    OK, first values are $firstKey, $firstVal

    " ) ; ####array_shift( $arr ) ; #'dequeue' front element # BAD! renumbers! unset( $arr[ $firstKey ] ) ; # BETTER! echo( "

    Now for the rest of them

    " ) ; foreach( $arr as $key=>$val ) { echo( "

    $key => $val

    " ) ; } ?>
    0 讨论(0)
  • 2020-12-03 04:47

    in loop:

    if ($key == 0) //or whatever
       continue;
    
    0 讨论(0)
  • 2020-12-03 04:49

    Couldn't you just unset the array...

    So if I had an array where I didn't want the first instance, I could just:

    unset($array[0]);
    

    and that would remove the instance from the array.

    0 讨论(0)
  • 2020-12-03 04:51
    foreach(array_slice($_POST['info'], 1) as $key=>$value) {
        echo $value;
    }
    

    Alternatively if you don't want to copy the array you could just do:

    $isFirst = true;
    foreach($_POST['info'] as $key=>$value) {
        if ($isFirst) {
            $isFirst = false;
            continue;
        }   
        echo $value;
    }
    
    0 讨论(0)
提交回复
热议问题