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

后端 未结 12 1365
情歌与酒
情歌与酒 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: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!).

         "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

    " ) ; } ?>

提交回复
热议问题