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

  • Home
  • SEARCH
  • 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 610041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:37:55+00:00 2026-05-13T17:37:55+00:00

This might sounds like a silly question. How do I get the 1st value

  • 0

This might sounds like a silly question. How do I get the 1st value of an array without knowing in advance if the array is associative or not?

In order to get the 1st element of an array I thought to do this:

function Get1stArrayValue($arr) { return current($arr); }

is it ok?
Could it create issues if array internal pointer was moved before function call?
Is there a better/smarter/fatser way to do it?

Thanks!

  • 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-13T17:37:55+00:00Added an answer on May 13, 2026 at 5:37 pm

    A better idea may be to use reset which “rewinds array’s internal pointer to the first element and returns the value of the first array element”

    Example:

    function Get1stArrayValue($arr) { return reset($arr); }
    

    As @therefromhere pointed out in the comment below, this solution is not ideal as it changes the state of the internal pointer. However, I don’t think it is much of an issue as other functions such as array_pop also reset it.
    The main concern that it couldn’t be used when iterating over an array isn’t an problem as foreach operates on a copy of the array. The PHP manual states:

    Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself.

    This can be shown using some simple test code:

    $arr = array("a", "b", "c", "d");
    foreach ( $arr as $val ){
        echo reset($arr) . " - " . $val . "\n";
    }
    

    Result:

    a - a
    a - b
    a - c
    a - d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 301k
  • Answers 301k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To create the overlay window you have to subclass NSWindow… May 13, 2026 at 8:10 pm
  • Editorial Team
    Editorial Team added an answer I have to agree with hobodave that it would probably… May 13, 2026 at 8:10 pm
  • Editorial Team
    Editorial Team added an answer Use: public JsonResult Submit([Bind(Prefix = "audience")]Audience aud,[Bind(Prefix = "locale")]Locale loc)… May 13, 2026 at 8:10 pm

Related Questions

I would like to create an application that runs on XBox 360 (downloaded using
I been working the whole week to troubleshot a production error. I have eventually
I realize this question might not be that programming related, and that it by
I am trying to understand conceptually the best way to deliver real streaming audio
I'm trying to play a PCM_UNSIGNED 11025.0 Hz, 8 bit, mono, 1 bytes/frame file

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.