title: Array key exists in multidimensional array
body: <p>I'm trying to arrange a group of pages in to an array and place them depending on their parent id number. If the parent id is 0 I would like it to be placed in the array as an array like so...</p>
  
  <pre><code>$get_pages = 'DATABASE QUERY'
  $sorted = array()
  
  foreach($get_pages as $k =&gt; $obj) {
      if(!$obj-&gt;parent_id) {
          $sorted[$obj-&gt;parent_id] = array();
      }
  }
  </code></pre>
  
  <p>But if the parent id is set I'd like to place it in to the relevant array, again as an array like so...</p>
  
  <pre><code>$get_pages = 'DATABASE QUERY'
  $sorted = array()
  
  foreach($get_pages as $k =&gt; $obj) {
      if(!$obj-&gt;parent_id) {
          $sorted[$obj-&gt;id] = array();
      } else if($obj-&gt;parent_id) {
          $sorted[$obj-&gt;parent_id][$obj-&gt;id] = array();
      }
  }
  </code></pre>
  
  <p>This is where I begin to have a problem. If I have a 3rd element that needs to be inserted to the 2nd dimension of an array, or even a 4th element that needs inserting in the 3rd dimension I have no way of checking if that array key exists. So what I can't figure out is how to detect if an array key exists after the 1st dimension and if it does where it is so I can place the new element.</p>
  
  <p>Here is an example of my Database Table</p>
  
  <pre><code>id    page_name    parent_id
  
  1     Products             0
  2     Chairs               1
  3     Tables               1
  4     Green Chairs         2
  5     Large Green Chair    4
  6     About Us             0
  </code></pre>
  
  <p>Here is an example of the output I'd like to get, if there is a better way to do this I'm open for suggestions.</p>
  
  <pre><code>Array([1]=&gt;Array([2] =&gt; Array([4] =&gt; Array([5] =&gt; Array())), [3] =&gt; Array()), 6 =&gt; Array())
  </code></pre>
  
  <p>Thanks in advanced!</p>