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 7434311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:48:56+00:00 2026-05-29T09:48:56+00:00

In a for loop it is simple… for ( $idx = 0 ; $idx

  • 0

In a for loop it is simple…

for ( $idx = 0 ; $idx < count ( $array ) ; $idx ++ )
{
    if ( $idx == 0 )
    {
        // This is the first element of the array.
    }
}

How the hell is this done in a foreach loop?

is there a function like is_first() or something?

I’m looking for something like:

foreach ( $array as $key => $value )
{
    if ( /* is the first element */ )
    {
        // do logic on first element
    }
    else
    {
        // all other logic
    }
}

I was thinking I could set a bool like $is_first = true; and then as soon as the loops been iterated once, set the bool to false.

But php has a lot of pre-built functions and id rather use that… or another way…

The whole bool way seem almost like… cheeting :s

Cheers,

Alex

  • 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-29T09:48:57+00:00Added an answer on May 29, 2026 at 9:48 am

    You can do this using “current()”

    $myArray = array('a', 'b', 'c');
    if (current($myArray) == $myArray[0]) {
        // We are at the first element
    }
    

    Docs: http://php.net/manual/en/function.current.php

    Ways of retrieving the first element:

    $myArray[0]
    
    $slice = array_slice($myArray, 0, 1); 
    $elm = array_pop($slice);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple loop: for($i=$_POST['position'];$i<count($myFiles);$i++) { $withoutNumber = explode(_,$myFiles[$i]); $noNr = unset($withoutNumber[0]); }
I have this simple loop in my program: for (Element *e in items) {
I have this simple loop: foreach ($links as $link) { echo '<div>'; echo '<table>';
I just designed a simple For Loop using window form application. I would like
Here's a simple method with a foreach loop: IEnumerable<XElement> FieldsToXElements(object instance) { var fieldElements
I am having trouble with this simple loop for a uni lab. It wont
I want to know how to replace a simple foreach loop with linq. I'm
I know you can rewrite a recursive function using a simple loop by using
I want to use a simple loop like for(int i=0; i<10; i++){} . How
take this simple loop while(1) { $data = file_get_contents('randomfiles.img'); $resource = imagecreatefromstring($data); //> do

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.