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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:42:34+00:00 2026-06-15T05:42:34+00:00

look at this: <?php $array = array(‘a’ => ‘…’, ‘b’ => ‘…’, ‘c’ =>

  • 0

look at this:

<?php
    $array = array('a' => '…', 'b' => '…', 'c' => '…', 'd' => '…', 'e' => '…', 'f' => '…');
    foreach ($array as $key => $val){
        echo "current key: $key, next key: ".key(($array))."<br>";
    }
?>

OUTPUT:

current key: a, next key: b
current key: b, next key: c
current key: c, next key: d
current key: d, next key: e
current key: e, next key: f
current key: f, next key: a

I was searching for a function to get the next key of an associative array within a foreach-loop. i tried a bit and suddenly it worked. (as you can see in my example).

BUT WHY DOES THIS WORK? Does it make sense? … not to me!
Can you explain this to me?

It’s because of the key(($array)) part but why? i mean.. it was a mistake.. i wanted to write key($array) but I forgot to delete the 2 wrapping brackets.
So it was coincidence !!!

Why does it behave this way? i mean, it’s good but … ????

  • 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-06-15T05:42:35+00:00Added an answer on June 15, 2026 at 5:42 am

    According to the PHP Manual for key, key() returns the index element of the current array position.

    The problem isn’t so much with key, or even with the double parentheses. Key receives the array by reference, so the double parentheses aren’t doing much.

    The behavior comes from foreach. When foreach iterates through the array, different versions of PHP will behave different on setting the array’s internal current pointer, which is what key(), next(), current(), etc, are looking at when they are called.

    Arrays in PHP aren’t like arrays in most languages; they are really objects (especially associative arrays). Think of them kinda like linked lists (they are not linked lists, but just go with me for illustration purposes) – when you iterate through, you need to know where you are currently at and where you are going to be next.

    What is apparently happening here is that on whatever version of PHP you are running, foreach is setting the internal current pointer to the next element at the beginning of the for loop, immediately after setting the $key and $value variables in your code.

    I would definitely not depend on this behavior, as subsequent updates to PHP may break this code. It’s just a fun coincidence of this specific version. If you want the next key, look at replacing your foreach loop. The PHP manual on next() has good examples, and be sure to also check out prev(), each(), and the other functions in the "see also" section.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is how I got my php array to javascript echo 'var daysofweek =
This is how I got my php array to javascript echo 'var daysofweek =
ok so i have this array and i need to loop through <?php foreach($classes
I have a directory with files that look like this: 001_something.php 002_something_else.php 004_xyz.php 005_do_good_to_others.php
I am converting a look-up table in PHP which looks like this to JavaScript
Look at this: $('form).submit(function (event) { $(':input.required').trigger('blur'); var num = $('.warning', this).length; alert(num);//Focus here!
look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {
I currently have two arrays that look like this: Swatches: Array ( [0] =>
I hava a function that looks something like this: require(config.php); function displayGta() { (...
I want to create a multidimensional array that will eventually look like this: s

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.