title: Loop recursively through multidimensional array and do something for every key value
body: <p>I am building a generic database seeder, where based upon given input (array) some tables are seeded. Currently I have an array that is nested 1 level deep:</p>
  
  <pre><code> $topics = [
       'Array value 1',
       'Array value 2',
       'Array key value 1' =&gt; [
            'Nested array value 1',
            'Nested array value 2',
       ],
       'Array key value 2' =&gt; [
            'Nested array value 3',
       ],
   ];
  </code></pre>
  
  <p>In my seeder I loop through this array of Topics. If the Topic is a key, it is a parent and I need to save its ID to assign it to the child. For example: the ID of 'Array key value 1' needs to be passed to 'Nested array value 1' because they are connected. The way I do this now is like this:</p>
  
  <pre><code>        // Define seeders.
          $topicSeeder = new TopicTableSeeder;
          $agendaPointSeeder = new AgendaPointTableSeeder;
  
          // Get all the Topics and create Agenda Points.
          foreach ($topics as $key =&gt; $topic) {
  
              // Check if Topic is an array.
              if (is_array($topic)) {
                  // If Topic is a key, it is a Parent.
                  if ($key) {
                      // Topic is Parent so set parentId to NULL and save own ID. 
                      $parentAgendaPointId = NULL;
  
                      // Execute Topic and Agenda Point seeders. 
                      $topicId = $topicSeeder-&gt;run($organizationId, $key);
                      $parentAgendaPointId = $agendaPointSeeder-&gt;run($parentAgendaPointId);                       
  
                  }
  
                  // If Topic is an array but not a key, it is a Child and gets the parentId. 
                  for ($i = 0; $i &lt; count($topic); $i++) {
                      $topicId = $topicSeeder-&gt;run($organizationId, $topic[$i]);
                      $agendaPointSeeder-&gt;run($parentAgendaPointId);
                  }
              } else {
                  // Topic isn't an array so it should have no Parent.
                  $parentAgendaPointId = NULL;
                  $topicId = $topicSeeder-&gt;run($topic);
                  $agendaPointSeeder-&gt;run($parentAgendaPointId);
              }
          }
  </code></pre>
  
  <p>The problem is, when the array is nested more levels deep, the foreach in the code above doesn't work anymore. For example this array:</p>
  
  <pre><code>  $topics = [
       'Unnested topic',
       'Unnested topic 2',
       'Key topic' =&gt; [
            'Nested topic',
            'Nested topic 2',
       ],
       'Key topic 2' =&gt; [
             'Nested key topic' =&gt; [
                 'Nested topic 3',
             ],
       ],
    ];
  </code></pre>
  
  <p>How can I make this recursive, but remain the ability to do something when it's a child value? Any ideas or suggestions are welcome because I'm a bit stuck now.  </p>