title: Sort stdClass by parent_id, parent_id~>page_id, then page_order
body: <p>I am trying to sort a navigation menu list, that I am being provided(as a $response in an array). So, it would be by parent_id (null is the top tier nav.) then the sub pages would fall in under the parent id's which would be page_id then in the page_order. Basically in the order that you would see the Nav. Menu on a website.</p>
  
  <p>For example:</p>
  
  <pre><code>First Nav
  --First Nav Sub Page 1
  --First Nav Sub Page 2
  --First Nav Sub Page 3
  --First Nav Sub Page 4
  --First Nav Sub Page 5
  Second Nav
  Third Nav
  --Third Nav Sub Page 1
  --Third Nav Sub Page 2
  Fourth Nav
  </code></pre>
  
  <p>The Array:</p>
  
  <pre><code>Array (
  [0] =&gt; stdClass Object
      (
          [page_id] =&gt; 122021
          [title] =&gt; First Nav
          [page_order] =&gt; 1
          [parent_id] =&gt; null
      )
  
  [1] =&gt; stdClass Object
      (
          [page_id] =&gt; 99122
          [title] =&gt; Second Nav
          [page_order] =&gt; 2
          [parent_id] =&gt; null
      )
  
  [2] =&gt; stdClass Object
      (
          [page_id] =&gt; 260447
          [title] =&gt; Third Nav
          [page_order] =&gt; 3
          [parent_id] =&gt; null
      )
  
  [3] =&gt; stdClass Object
      (
          [page_id] =&gt; 170627
          [title] =&gt; Fourth Nav
          [page_order] =&gt; 4
          [parent_id] =&gt; null
      )
  
  [4] =&gt; stdClass Object
      (
          [page_id] =&gt; 134788
          [title] =&gt; Third Nav Sub Page 1
          [page_order] =&gt; 1
          [parent_id] =&gt; 260447
      )
  
  [5] =&gt; stdClass Object
      (
          [page_id] =&gt; 157074
          [title] =&gt; Third Nav Sub Page 2
          [page_order] =&gt; 2
          [parent_id] =&gt; 260447
      )
  
  [6] =&gt; stdClass Object
      (
          [page_id] =&gt; 699423
          [title] =&gt; First Nav Sub Page 1
          [page_order] =&gt; 1
          [parent_id] =&gt; 122021
      )
  
  [7] =&gt; stdClass Object
      (
          [page_id] =&gt; 346209
          [title] =&gt; First Nav Sub Page 2
          [page_order] =&gt; 2
          [parent_id] =&gt; 122021
      )
  
  [8] =&gt; stdClass Object
      (
          [page_id] =&gt; 732773
          [title] =&gt; First Nav Sub Page 3
          [page_order] =&gt; 3
          [parent_id] =&gt; 122021
      )
  
  [9] =&gt; stdClass Object
      (
          [page_id] =&gt; 164338
          [title] =&gt; First Nav Sub Page 4
          [page_order] =&gt; 4
          [parent_id] =&gt; 122021
      )
  
  [10] =&gt; stdClass Object
      (
          [page_id] =&gt; 671117
          [title] =&gt; First Nav Sub Page 5
          [page_order] =&gt; 5
          [parent_id] =&gt; 122021
      )
  )
  </code></pre>
  
  <p>I have managed to get this in a partial order, by parent_id then page_order (as shown in the array above) using the ArrSort function noted below, but I have not been able to put the sub pages page_order 'underneath' their parent_id. </p>
  
  <p>I have tried: </p>
  
  <pre><code>function ArrSort ($Array){
     $parent_id = array();
     $page_order = array();
     foreach($Array as $key =&gt; $value){
         $parent_id[] = $value-&gt;parent_id;
         $page_order[] = $value-&gt;page_order;
     }
     array_multisort($parent_id, SORT_ASC, $page_order, SORT_ASC, $Array);
     return $Array;
  }
  $sorted_pages_array = ArrSort($response);
  </code></pre>
  
  <p>I have also tried the following, </p>
  
  <pre><code>function PageList($a, $b){
    if ( $a-&gt;page_id == $b-&gt;page_id ) {
      return 0;
    } else if ( $a-&gt;parent_id ) {
        if ( $a-&gt;parent_id == $b-&gt;parent_id ) {
           return ( $a-&gt;page_id &lt; $b-&gt;page_id ? -1 : 1 );
        } else {
           return ( $a-&gt;parent_id &gt;= $b-&gt;page_id ? 1 : -1 );
        }
    } else if ( $b-&gt;parent_id ) {
      return ( $b-&gt;parent_id &gt;= $a-&gt;page_id ? -1 : 1);
    } else {
      return ( $a-&gt;page_id &lt; $b-&gt;page_id ? -1 : 1 );
    }
  }
  usort($response, "PageList");
  </code></pre>
  
  <p>And a combination of both:</p>
  
  <pre><code>$sorted_pages_array = ArrSort($response);
  usort($sorted_pages_array, "PageList");
  </code></pre>
  
  <p>Unfortunately I am not that familiar with arrays, and sorting, and could use some assistance.</p>