Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 495379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:35:16+00:00 2026-05-13T05:35:16+00:00

Is there a way to specify getting all but the first element in an

  • 0

Is there a way to specify getting all but the first element in an array? I generally use foreach() to loop through my arrays.

say array(1,2,3,4,5), i would only want 2, 3, 4 ,5 to show and for it to skip 1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:35:16+00:00Added an answer on May 13, 2026 at 5:35 am

    There are multiple ways of approaching this problem.

    The first solution is to use a flag boolean to indicate the first element and proceed in your foreach

    $firstElement = true;
    
    foreach($array as $key => $val) {
      if($firstElement) {
        $firstElement = false;
      } else {
        echo "$key => $val\n";
      }
    }
    

    If your elements are naturally numerically indexed, you do not need the boolean flag, you can simply check if the key is 0.

    foreach($array as $key => $val) {
      if($key === 0) continue;      
    
      echo "$key => $val\n";
    }
    

    The second way is to cheat your way into a naturally numerically indexed array if it isn’t already. I will use array_keys() to get a naturally numerically indexed array of keys and loop it.

    $keys = array_keys($array);
    
    foreach($keys as $index => $key) {
      if($index === 0) continue;   
    
      $val = $array[$key];
      echo "$key => $val\n";
    }
    

    The third way is to use the array internal pointer to skip the first element and then continue in a loop by using reset(), next(), list(), and each(). Performance and resource-wise, this is the best option. Maintainability suffers greatly though.

    reset($array); // Reset pointer to 0
    next($array);  // Advance pointer to 1
    
    while (list($key, $val) = each($array)) {
      echo "$key => $val\n";
    }  
    

    If you don’t mind losing the first element of the array, you can array_shift() it.

    array_shift($array);
    
    foreach($array as $key => $val) {
      echo "$key => $val\n";
    }
    

    You can also array_slice() the array. I’m also using count() in order to be able to set the preserve_keys parameter to true.

    $sliced = array_slice($array, 1, count($array)-1, true);
    
    foreach($sliced as $key => $val) {
      echo "$key => $val\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am outputting a query but need to specify the first row of the
I am getting contents of all views (Folders).Like Inbox,Calendar,ToDo e.t.c. As mentioned in Title
First, a couple operating parameters: .NET development using Visual Studio 2005/2008 TortoiseSVN client I've
I have a project to update all reports on an SSRS instance and thought
I have a WinForms app built using Visual Studio 2005, including the crystal reports
I want my website to join some webcam recordings in FLV files (like this
I thought the C/C++ vs C#/Java performance question was well trodden, meaning that I'd
FxCop is currently reporting the same rule violation for a particular method -- it
We have a updatable web site project that is written in c#, it has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.