<p>I have the following code snippet.</p>

<pre><code>$items['A'] = "Test";
$items['B'] = "Test";
$items['C'] = "Test";
$items['D'] = "Test";

$index = 0;
foreach($items as $key =&gt; $value)
{
    echo "$index is a $key containing $value\n";
    $index++;
}
</code></pre>

<p>Expected output:</p>

<pre><code>0 is a A containing Test
1 is a B containing Test
2 is a C containing Test
3 is a D containing Test
</code></pre>

<p>Is there a  way to leave out the <code>$index</code> variable?</p>

[{'answer_id': 68612, 'author': 'Brent', 'author_id': 10680, 'author_profile': 'https://Stackoverflow.com/users/10680', 'pm_score': 5, 'selected': True, 'text': '<p>Your $index variable there kind of misleading. That number isn\'t the index, your "A", "B", "C", "D" keys are. You can still access the data through the numbered index $index[1], but that\'s really not the point. If you really want to keep the numbered index, I\'d almost restructure the data:</p>\n\n<pre>\n$items[] = array("A", "Test");\n$items[] = array("B", "Test");\n$items[] = array("C", "Test");\n$items[] = array("D", "Test");\n\nforeach($items as $key => $value) {\n    echo $key.\' is a \'.$value[0].\' containing \'.$value[1];\n}\n</pre>\n'}, {'answer_id': 68647, 'author': 'dawnerd', 'author_id': 69503, 'author_profile': 'https://Stackoverflow.com/users/69503', 'pm_score': 2, 'selected': False, 'text': '<p>You can do this:</p>\n\n<pre><code>$items[A] = "Test";\n$items[B] = "Test";\n$items[C] = "Test";\n$items[D] = "Test";\n\nfor($i=0;$i&lt;count($items);$i++)\n{\n    list($key,$value) = each($items[$i]);\n    echo "$i $key contains $value";\n}\n</code></pre>\n\n<p>I haven\'t done that before, but in theory it should work.</p>\n'}, {'answer_id': 68918, 'author': 'nickf', 'author_id': 9021, 'author_profile': 'https://Stackoverflow.com/users/9021', 'pm_score': 1, 'selected': False, 'text': '<p>Be careful how you\'re defining your keys there. While your example works, it might not always:</p>\n\n<pre><code>$myArr = array();\n$myArr[A] = "a";  // "A" is assumed.\necho $myArr[\'A\']; // "a" - this is expected.\n\ndefine (\'A\', \'aye\');\n\n$myArr2 = array();\n$myArr2[A] = "a"; // A is a constant\n\necho $myArr[\'A\']; // error, no key.\nprint_r($myArr);\n\n// Array\n// (\n//     [aye] =&gt; a\n// )\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/68583', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/264/']