<p>Is there an easy way to iterate over an associative array of this structure in PHP:</p>

<p>The array <code>$searches</code> has a numbered index, with between 4 and 5 associative parts. So I not only need to iterate over <code>$searches[0]</code> through <code>$searches[n]</code>, but also <code>$searches[0]["part0"]</code> through <code>$searches[n]["partn"]</code>. The hard part is that different indexes have different numbers of parts (some might be missing one or two).</p>

<p>Thoughts on doing this in a way that's nice, neat, and understandable?</p>

[{'answer_id': 26013, 'author': 'Re0sless', 'author_id': 2098, 'author_profile': 'https://Stackoverflow.com/users/2098', 'pm_score': 3, 'selected': False, 'text': '<p>You should be able to use a nested foreach statment</p>\n\n<p>from the <a href="http://uk3.php.net/foreach" rel="noreferrer">php manual</a></p>\n\n<pre><code>/* foreach example 4: multi-dimensional arrays */\n$a = array();\n$a[0][0] = "a";\n$a[0][1] = "b";\n$a[1][0] = "y";\n$a[1][1] = "z";\n\nforeach ($a as $v1) {\n    foreach ($v1 as $v2) {\n        echo "$v2\\n";\n    }\n}\n</code></pre>\n'}, {'answer_id': 26014, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 0, 'selected': False, 'text': '<p>Can you just loop over all of the "part[n]" items and use isset to see if they actually exist or not?</p>\n'}, {'answer_id': 26018, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 6, 'selected': True, 'text': '<p>Nest two <a href="http://php.net/foreach" rel="noreferrer"><code>foreach</code> loops</a>:</p>\n\n<pre><code>foreach ($array as $i =&gt; $values) {\n    print "$i {\\n";\n    foreach ($values as $key =&gt; $value) {\n        print "    $key =&gt; $value\\n";\n    }\n    print "}\\n";\n}\n</code></pre>\n'}, {'answer_id': 26023, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 0, 'selected': False, 'text': "<p>I'm really not sure what you mean here - surely a pair of foreach loops does what you need?</p>\n\n<pre><code>foreach($array as $id =&gt; $assoc)\n{\n    foreach($assoc as $part =&gt; $data)\n    {\n        // code\n    }\n}\n</code></pre>\n\n<p>Or do you need something recursive? I'd be able to help more with example data and a context in how you want the data returned.</p>\n"}, {'answer_id': 177476, 'author': 'Milan Babuškov', 'author_id': 14690, 'author_profile': 'https://Stackoverflow.com/users/14690', 'pm_score': 4, 'selected': False, 'text': "<p>Looks like a good place for a recursive function, esp. if you'll have more than two levels of depth.</p>\n\n<pre><code>function doSomething(&amp;$complex_array)\n{\n    foreach ($complex_array as $n =&gt; $v)\n    {\n        if (is_array($v))\n            doSomething($v);\n        else\n            do whatever you want to do with a single node\n    }\n}\n</code></pre>\n"}, {'answer_id': 2149106, 'author': 'Gordon', 'author_id': 208809, 'author_profile': 'https://Stackoverflow.com/users/208809', 'pm_score': 5, 'selected': False, 'text': '<p>I know it\'s question necromancy, but iterating over Multidimensional arrays is easy with Spl Iterators</p>\n\n<pre><code>$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));\n\nforeach($iterator as $key=&gt;$value) {\n    echo $key.\' -- \'.$value.\'&lt;br /&gt;\';\n}\n</code></pre>\n\n<p>See</p>\n\n<ul>\n<li><a href="http://php.net/manual/en/spl.iterators.php" rel="noreferrer">http://php.net/manual/en/spl.iterators.php</a></li>\n</ul>\n'}, {'answer_id': 12755040, 'author': 'mana', 'author_id': 939723, 'author_profile': 'https://Stackoverflow.com/users/939723', 'pm_score': 0, 'selected': False, 'text': "<p>Consider this multi dimentional array, I hope this function will help.</p>\n\n<pre><code>$n = array('customer' =&gt; array('address' =&gt; 'Kenmore street',\n                'phone' =&gt; '121223'),\n      'consumer' =&gt; 'wellington consumer',\n      'employee' =&gt; array('name' =&gt; array('fname' =&gt; 'finau', 'lname' =&gt; 'kaufusi'),\n                     'age' =&gt; 32,\n                 'nationality' =&gt; 'Tonga')\n      );\n\n\n\niterator($n);\n\nfunction iterator($arr){\n\n    foreach($arr as $key =&gt; $val){\n\n    if(is_array($val))iterator($val);\n\n    echo '&lt;p&gt;key: '.$key.' | value: '.$val.'&lt;/p&gt;';\n\n    //filter the $key and $val here and do what you want\n    }\n\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/26007', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/572/']