0

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?

denski
  • 1,768
  • 4
  • 17
  • 35
  • 1
    Do you mean `echo $index.": ".$row;` with the `.` before `$row?` – Albzi Feb 21 '17 at 10:45
  • 1
    Same way, `$index` will be the key name. – utnaf Feb 21 '17 at 10:46
  • 1
    @Albzi, spotted and edited it thx – denski Feb 21 '17 at 10:46
  • @Effe thx I must be doing something incorrectly so. – denski Feb 21 '17 at 10:47
  • What does your array look like? – Albzi Feb 21 '17 at 10:47
  • `$index => $row` the first variable will always return the index, no matter if it is integer or string – Fabio Feb 21 '17 at 10:47
  • `state[][Name]` `state[][ID]` `state[][Date]` – denski Feb 21 '17 at 10:48
  • Provide your associative array. – Muthu17 Feb 21 '17 at 10:48
  • 2
    if yoy want to print the second index you will need to loop again `foreach($row as $key => $val)` – Fabio Feb 21 '17 at 10:49
  • @fabio thanks for this. I think it's the second dimension I'm leaving out. – denski Feb 21 '17 at 10:52
  • yes, you should probably loop again, be aware if the arrayhave also value at second level you might want to check if `is_array()` before looping otherwise you might get a notice about that – Fabio Feb 21 '17 at 10:55
  • Duplicate of: http://stackoverflow.com/questions/842956/php-foreach-loop-through-multidimensional-array http://stackoverflow.com/questions/1551822/looping-a-multidimensional-array-in-php and http://stackoverflow.com/questions/17851340/iterating-through-multi-dimensional-arrays – mickmackusa Feb 21 '17 at 10:57
  • So this gets me the index number of state? `foreach ( $state as $key => $row )` While this nested would give me the index names? `Name, ID, Date` `foreach ( $row as $index => $subarray )` – denski Feb 21 '17 at 10:57
  • yeah that's correct – Fabio Feb 21 '17 at 10:59
  • @mickmackusa I can see why now but the correct phraseology for the question eluded me until I asked it. – denski Feb 21 '17 at 11:00
  • Someone with more experience/rank will come along and mark this question as a duplicate and close it. Or you can delete the question if you have the information you need already. (I've never actually flagged before) – mickmackusa Feb 21 '17 at 11:05

2 Answers2

1

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
VeRJiL
  • 415
  • 4
  • 13
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.

mroman
  • 1,354
  • 9
  • 14
  • 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 – Fabio Feb 21 '17 at 10:49
  • 1
    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. – mroman Feb 21 '17 at 10:51
  • Actually it doesn't and if you would read comment you would see op is asking for something else – Fabio Feb 21 '17 at 10:53
  • 1
    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. – mroman Feb 21 '17 at 10:56
  • 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. – mickmackusa Feb 21 '17 at 11:22
  • "If my array is associative is there away to get the associative name instead of the index number into my loop?". I explained that indexes are just keys as well and that perfectly answers the question, I provided a link to the documentation and even posted demo code. You literally can not answer a question any better than this. – mroman Feb 21 '17 at 13:00
  • If he meant "how can I loop over the keys of individual rows where rows is an array of arrays with string as keys" then that's a COMPLETELY different question and instead of completely altering a question he should imo actually post a NEW question. If questions are so fundamentally different then editing them is stupid imo. – mroman Feb 21 '17 at 13:03