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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:27:57+00:00 2026-05-23T14:27:57+00:00

Let S be an associative array in PHP, I need to retrieve and extract

  • 0

Let S be an associative array in PHP, I need to retrieve and extract from it the first element, both the value and the key.

I would use

value1=array_pop(S);

but it only gives me the value.

I can use

K=array_keys(S);
key1=array_pop(K);
value1=array_pop(S);

but it is complicated because it requires to have two copies of the same data. WHich is a confusing since the array is itself an element in an array of arrays. There must be a more elegant way to just read the couple key/value while extracting it.

  • 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-23T14:27:58+00:00Added an answer on May 23, 2026 at 2:27 pm
    // first
    $value = reset($arr);
    $key = key($arr);
    

    (in that order)

    See reset()PHP Manual,
    key()PHP Manual.

    unset($arr[$key]); # in case you want to remove it.
    

    However array_pop()PHP Manual is working with the last element:

    // last
    $value = end($arr);
    $key = key($arr);
    unset($arr[$key]); # in case you want to remove it.
    

    See end()PHP Manual.

    For the fun:

    [$value, $key] = [reset($arr), key($arr)]; // first
    [$value, $key] = [end($arr), key($arr)]; // last
    

    (PHP 7.1+)

    or

    list($value, $key) = array(reset($arr), key($arr)); // first
    list($value, $key) = array(end($arr), key($arr)); // last
    

    (PHP 4.3+)

    or

    extract(array('value' => reset($arr), 'key' => key($arr))); // first
    extract(array('value' => end($arr), 'key' => key($arr))); // last
    

    (PHP 4.3+; Caution: extract() in use!)

    or

    // first
    reset($arr);
    list($key, $value) = each($arr);
    
    
    // last
    end($arr);
    list($key, $value) = each($arr);
    

    (Note: The each() function is deprecated since PHP 7.2.0 and gone since PHP 8.0.0)

    or whatever style of play you like 😉

    Dealing with empty arrays

    It was missing so far to deal with empty arrays. So it’s a need to check if there is a last (first) element and if not, set the $key to null (as null can not be an array key):

    // first
    for ($key = null, $value = null; false !== $_ = reset($arr);)
    {
        $value = $_;
        unset($arr[$key = key($arr)]);
        break;
    }
    unset($_);
    
    // last
    for ($key = null, $value = null; false !== $_ = end($arr);)
    {
        $value = $_;
        unset($arr[$key = key($arr)]);
        break;
    }
    unset($_);
    

    This will give for a filled array like $arr = array('first' => '1st', 'last' => '2nd.');:

    string(4) "2nd." # value
    string(4) "last" # key
    array(1) { # leftover array
      ["first"]=>
      string(3) "1st"
    }
    

    And an empty array:

    bool(false) # value
    NULL # key
    array(0) { # leftover array
    }
    

    Afraid of using unset?

    In case you don’t trust unset() having the performance you need (of which I don’t think it’s really an issue, albeit I haven’t run any metrics), you can use the native array_pop() implementation as well (but I really think that unset() as a language construct might be even faster):

    // first
    reset($arr);
    $key = key($arr);
    $value = array_pop($arr);
    
    
    // last
    end($arr);
    $key = key($arr);
    $value = array_pop($arr);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an associative array like so: {'key1': 22, 'key2': 42} .
Let's say I have a Javascript associative array (a.k.a. hash, a.k.a. dictionary): var a
I have a JavaScript object that is treated as an associative array. Let's call
Let's say I have an associative array listing animals at the zoo and some
I'm not quite sure how to do this. Let's say I have 2 associative
Let me try to explain what I need. I have a server that is
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a
Let's say I'm building a library to spork quuxes in C. Quuxes need two
How can I copy an array that has other associative arrays in it? I
I need to sort an array containing a list of words and search the

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.