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

后端 未结 12 1361
情歌与酒
情歌与酒 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:51

    if you structure your form differently

      <input type='text' name='quiz[first]' value=""/>
      <input type='text' name='quiz[second]' value=""/>
    

    ...then in your PHP

    if( isset($_POST['quiz']) AND 
        is_array($_POST['quiz'])) {
    
        //...and we'll skip $_POST['quiz']['first'] 
        foreach($_POST['quiz'] as $key => $val){
          if($key == "first") continue;
          print $val; 
        }
    }
    

    ...you can now just loop over that particular structure and access rest normally

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

    Working Code From My Website For Skipping The First Result and Then Continue.

    <?php 
    
    $counter = 0;
    
    foreach ($categoriest as $category) { if ($counter++ == 0) continue; ?>
    

    It is working on opencart also in tpl file do like this in case you need.

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

    If you're willing to throw the first element away, you can use array_shift(). However, this is slow on a huge array. A faster operation would be

    reset($a);
    unset(key($a));
    
    0 讨论(0)
  • 2020-12-03 04:55

    Alternative way is to use array pointers:

    reset($_POST['info']); //set pointer to zero
    while ($value=next($_POST['info'])  //ponter+1, return value
    {
      echo key($_POST['info']).":".$value."\n";
    }
    
    0 讨论(0)
  • 2020-12-03 05:01

    If you were working with a normal array, I'd say to use something like

    foreach (array_slice($ome_array, 1) as $k => $v {...

    but, since you're looking at a user request, you don't have any real guarantees on the order in which the arguments might be returned - some browser/proxy might change its behavior or you might simply decide to modify your form in the future. Either way, it's in your best interest to ignore the ordering of the array and treat POST values as an unordered hash map, leaving you with two options :

    • copy the array and unset the key you want to ignore
    • loop through the whole array and continue when seeing the key you wish to ignore
    0 讨论(0)
  • 2020-12-03 05:04

    For reasonably small arrays, use array_slice to create a second one:

    foreach(array_slice($_POST['info'],1) as $key=>$value)
    {
        echo $value;
    }
    
    0 讨论(0)
提交回复
热议问题