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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:47:19+00:00 2026-06-14T19:47:19+00:00

I have an associative array. Two dimensions which I am iterating through like this

  • 0

I have an associative array. Two dimensions which I am iterating through like this

foreach ( $order_information as $sector_index => $sector_value ){
echo 'sector : ' . current($order_information) ;
echo '<br>';
    foreach ( $sector_value as $line_index => $line_value ){

    }
}

The current() is an attempt to get the iteration the loop is in. It seems like this should give me that. However, elsewhere on that page there is the suggestions that you just do like

$index = 0
foreach ( $array as $key => $val ) {
    echo $index;
    $index++;
} 

I wonder if I am using current incorrectly, as echo 'sector : ' . current($order_information); just prints sector : Array

Is $index++ bad syntax? Is there a better way to do this?

  • 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-14T19:47:20+00:00Added an answer on June 14, 2026 at 7:47 pm

    Answer

    As far as I know there is no build in numeric counter in a foreach loop in PHP.

    So you need your own counter variable. Your example code looks quite good to do that.

    $index = 0;
    foreach($array as $key => $val) {
        $index++;
    }
    

    By the way, $index++; is fine.

    Example

    Here is an example illustrating which variable stores which value.

    $array = array(
        "first"  => 100,
        "secnd" => 200,
        "third"  => 300,
    );
    
    $index = 0;
    foreach($array as $key => $val) {
        echo "index: " . $index . ", ";
        echo "key: "   . $key   . ", ";
        echo "value: " . $val   . "\n";
        $index++;
    }
    

    The output will be that.

    index: 0, key: first, value: 100
    index: 1, key: secnd, value: 200
    index: 2, key: third, value: 300
    

    Current-Function

    I think you misunderstood current($array). It gives you the value pointed by an internal array pointer, which can be moved using next($array), prev($array) and end($array).

    Take a look at the manual to make thinks clear.

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

Sidebar

Related Questions

I have an associative array which when var dumped looks like this: Array (
I have an associative array with two object inside. Running this through $(myassoc).each() ,
I have an associative array in awk that gets populated like this: chr_count[$3]++ When
I have an associative array assigned to a Smarty variable, something like this: $foo
I have a nested associative array like this: $inputTypes= array( natural => array( text,
I have a hierarchically nested associative array. It looks like this: A = {
In PHP, I have an associative array like this $a = array('who' => 'one',
I like to learn how to combine two associative array which has same keys
I have 'multidimensional associative' javascript array (which in fact is object with properties as
I have a multidimensional associative array, which I pass to a smarty template. I

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.