1

I can get the index number from a foreach loop by doing the following.

foreach ($rows as $index=>$row)
{
    echo $index.": ".$row;
    // gives me "1: $row etc 
}

If my array is associative is there away to get the associative name instead of the index number into my loop?

16
  • 1
    Do you mean echo $index.": ".$row; with the . before $row? Commented Feb 21, 2017 at 10:45
  • 1
    Same way, $index will be the key name. Commented Feb 21, 2017 at 10:46
  • 1
    @Albzi, spotted and edited it thx Commented Feb 21, 2017 at 10:46
  • @Effe thx I must be doing something incorrectly so. Commented Feb 21, 2017 at 10:47
  • 2
    if yoy want to print the second index you will need to loop again foreach($row as $key => $val) Commented Feb 21, 2017 at 10:49

2 Answers 2

2

Actually you allready did it:

$associativeArray = array(
    'First'  => 1,
    'Second' => 2,
    'Third'  => 3,
); 
foreach ($associativeArray as $index => $value) {
    echo $index . ": " . $value;
}
    // First:  1
    // Second: 2
    // Third:  3
Sign up to request clarification or add additional context in comments.

Comments

0
<?
$rows = array();
$rows['hi'] = 'there';
$rows['foo'] = 'bar';
foreach ($rows as $index=>$row)
{
    echo $index.": ".$row;
    // $index will be hi and foo
}
?>

PHP arrays ARE associative where regular arrays just have integers as keys.

The PHP documentation actually mentions this in the first sentence: http://php.net/manual/en/language.types.array.php

An array in PHP is actually an ordered map.. PHP doesn't have arrays, it has maps/dictionaries that are called arrays but they are not arrays like in other languages.

7 Comments

This should really really be not posted, you might want to comment instead. This answer is no sense nor answer the op. As side note short php tag should not be used anymore
It perfectly answers the question. It shows how you get the key instead of the index as there's not even a difference because PHP doesn't have the kind of arrays he thinks they have.
Actually it doesn't and if you would read comment you would see op is asking for something else
So. It still answers the original question. If the original question was asked wrong how do you expect me to answer it then? If he asks how to do 'X' and then later in the comments changes it to something else while I answer the question that was asked how is that wrong? Besides, when people later use the search to search for question what they will see is the title of the question and my answer is still correct because most will not read through all the comment to notice that he was asking something completely different.
All questions and answers SHOULD be edited to reflect the best / most recent information so that it doesn't confuse/mislead future readers. There is no shame in editing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.