title: PHP - foreach loop and multidimensional array
body: <p>There a lots of questions/answers here on SO about multidim arrays and foreach loops but I haven't found one that applies specifically to my case. Please advise if you think otherwise.</p>
  
  <p>My array is structured as below, and is output to a CodeIgniter view:</p>
  
  <pre><code>Array
  (
      [2] =&gt; Array //this is the unique user ID
          (
              [name] =&gt; Joe Schmoe
              [cars] =&gt; Array
                  (
                      [112] =&gt; Array //this is the unique car ID
                          (
                              [cars_name] =&gt; Honda
                              [cars_type] =&gt; Sedan
                              [cars_color] =&gt; White
                          )
  
                      [102] =&gt; Array
                          (
                              [cars_name] =&gt; Toyota
                              [cars_type] =&gt; Sedan
                              [cars_color] =&gt; Black
                          )
  
                      [113] =&gt; Array
                          (
                              [cars_name] =&gt; Nissan
                              [cars_type] =&gt; Coupe
                              [cars_color] =&gt; Red
                          )
                  )
          )
  
      [5] =&gt; Array
          (
              [name] =&gt; Buck
              [cars] =&gt; Array
                  (
                      [147] =&gt; Array
                          (
                              [cars_name] =&gt; Tesla
                              [cars_type] =&gt; Sedan
                              [cars_color] =&gt; Yellow
                          )
                  )
          )
  
      [1] =&gt; Array
          (
              [name] =&gt; Mike Mechanic
              [cars] =&gt; Array
                  (
                      [140] =&gt; Array
                          (
                              [cars_name] =&gt; BMW
                              [cars_type] =&gt; SUV
                              [cars_color] =&gt; Blue
                          )
  
                      [145] =&gt; Array
                          (
                              [cars_name] =&gt; MB
                              [cars_type] =&gt; Sedan
                              [cars_color] =&gt; Gray
                          )
  
                  )
          )
  )
  </code></pre>
  
  <p>I need help building the nested <code>foreach</code> loops so I can have access to each one of the elements in the entire array.</p>
  
  <p>I tried this nested <code>foreach</code> structure but it's a no go.</p>
  
  <pre><code>foreach ($results as $data):
      if (is_array($data)):
          foreach ($data as $value):
              if (is_array($value)):
                  foreach ($value as $row):
                  endforeach;
              endif;
          endforeach;
      endif;
  endforeach;
  
  echo $row-&gt;cars_name //returns a non-object error
  </code></pre>
  
  <p>Anyone know how to put this nested loop together?</p>
  
  <p>Thanks for helping, much appreciated.</p>