title: PHP - Convert an array into a multidimensional json object
body: <p>I have a query <code>SELECT * FROM NavigationMenu ORDER BY parentNavigationMenuId ASC, navigationOrder ASC</code> that will return an array of nav objects.  The result i have to work with looks like this:</p>
  
  <pre><code>array (
    0 =&gt; 
    array (
      'navigationMenuId' =&gt; '1',
      'parentNavigationMenuId' =&gt; '0',
      'fullURL' =&gt; '/index',
      'contentSlug' =&gt; 'index',
      'contentAlias' =&gt; 'Index',
      'navigationAnchor' =&gt; 'Home',
      'navigationOrder' =&gt; '1',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    1 =&gt; 
    array (
      'navigationMenuId' =&gt; '2',
      'parentNavigationMenuId' =&gt; '0',
      'fullURL' =&gt; '/top-nav-1/index',
      'contentSlug' =&gt; 'index',
      'contentAlias' =&gt; 'Index',
      'navigationAnchor' =&gt; 'Top Nav 1',
      'navigationOrder' =&gt; '2',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    2 =&gt; 
    array (
      'navigationMenuId' =&gt; '3',
      'parentNavigationMenuId' =&gt; '0',
      'fullURL' =&gt; '/top-nav-2/index',
      'contentSlug' =&gt; 'index',
      'contentAlias' =&gt; 'Index',
      'navigationAnchor' =&gt; 'Top Nav 2',
      'navigationOrder' =&gt; '3',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    3 =&gt; 
    array (
      'navigationMenuId' =&gt; '8',
      'parentNavigationMenuId' =&gt; '0',
      'fullURL' =&gt; '/top-nav-3/index',
      'contentSlug' =&gt; 'index',
      'contentAlias' =&gt; 'Top Nav 3',
      'navigationAnchor' =&gt; 'Top Nav 3',
      'navigationOrder' =&gt; '4',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    4 =&gt; 
    array (
      'navigationMenuId' =&gt; '9',
      'parentNavigationMenuId' =&gt; '0',
      'fullURL' =&gt; '/top-nav-4/index',
      'contentSlug' =&gt; 'index',
      'contentAlias' =&gt; 'Top Nav 4',
      'navigationAnchor' =&gt; 'Top Nav 4',
      'navigationOrder' =&gt; '5',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    5 =&gt; 
    array (
      'navigationMenuId' =&gt; '13',
      'parentNavigationMenuId' =&gt; '0',
      'fullURL' =&gt; '/top-nav-5/index',
      'contentSlug' =&gt; 'index',
      'contentAlias' =&gt; 'Top Nav 5',
      'navigationAnchor' =&gt; 'Top Nav 5',
      'navigationOrder' =&gt; '6',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    6 =&gt; 
    array (
      'navigationMenuId' =&gt; '4',
      'parentNavigationMenuId' =&gt; '3',
      'fullURL' =&gt; '/top-nav-2/sub-nav-1',
      'contentSlug' =&gt; 'sub-nav-1',
      'contentAlias' =&gt; 'Sub Nav 1',
      'navigationAnchor' =&gt; 'Sub Nav 1',
      'navigationOrder' =&gt; '1',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    7 =&gt; 
    array (
      'navigationMenuId' =&gt; '5',
      'parentNavigationMenuId' =&gt; '3',
      'fullURL' =&gt; '/top-nav-2/sub-nav-2',
      'contentSlug' =&gt; 'sub-nav-2',
      'contentAlias' =&gt; 'Sub Nav 2',
      'navigationAnchor' =&gt; 'Sub Nav 2',
      'navigationOrder' =&gt; '2',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    8 =&gt; 
    array (
      'navigationMenuId' =&gt; '6',
      'parentNavigationMenuId' =&gt; '3',
      'fullURL' =&gt; '/top-nav-2/sub-nav-3',
      'contentSlug' =&gt; 'sub-nav-3',
      'contentAlias' =&gt; 'Sub Nav 3',
      'navigationAnchor' =&gt; 'Sub Nav 3',
      'navigationOrder' =&gt; '3',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    9 =&gt; 
    array (
      'navigationMenuId' =&gt; '7',
      'parentNavigationMenuId' =&gt; '3',
      'fullURL' =&gt; '/top-nav-2/sub-nav-4',
      'contentSlug' =&gt; 'sub-nav-4',
      'contentAlias' =&gt; 'Sub Nav 4',
      'navigationAnchor' =&gt; 'Sub Nav 4',
      'navigationOrder' =&gt; '4',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    10 =&gt; 
    array (
      'navigationMenuId' =&gt; '10',
      'parentNavigationMenuId' =&gt; '9',
      'fullURL' =&gt; '/top-nav-4/sub-nav-1',
      'contentSlug' =&gt; 'sub-nav-1',
      'contentAlias' =&gt; 'Sub Nav 1',
      'navigationAnchor' =&gt; 'Sub Nav 1',
      'navigationOrder' =&gt; '1',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    11 =&gt; 
    array (
      'navigationMenuId' =&gt; '11',
      'parentNavigationMenuId' =&gt; '9',
      'fullURL' =&gt; '/top-nav-4/sub-nav-2',
      'contentSlug' =&gt; 'sub-nav-2',
      'contentAlias' =&gt; 'Sub Nav 2',
      'navigationAnchor' =&gt; 'Sub Nav 2',
      'navigationOrder' =&gt; '2',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
    12 =&gt; 
    array (
      'navigationMenuId' =&gt; '12',
      'parentNavigationMenuId' =&gt; '9',
      'fullURL' =&gt; '/top-nav-4/sub-nav-3',
      'contentSlug' =&gt; 'sub-nav-3',
      'contentAlias' =&gt; 'Sub Nav 3',
      'navigationAnchor' =&gt; 'Sub Nav 3',
      'navigationOrder' =&gt; '3',
      'subNavigationItems' =&gt; 
      array (
      ),
    ),
  )
  </code></pre>
  
  <p>Any one of these objects can have child nav items (a child can have other children and so on) which is why each nav item has an empty array of <code>subNavigationItems</code>.  I basically want to use the <code>navigationMenuId</code> and <code>parentNavigationMenuId</code> properties and build a json object that has the correct children nested under the correct parents.  I'm assuming it could be done with a series of loops for the logic of it is giving me a headache.  Anyone have any thoughts on how this can be achieved?  Here is an example json of what i would like (or something similar):</p>
  
  <pre><code>    [
          {
              "navigationMenuId": "1",
              "parentNavigationMenuId": "0",
              "fullURL": "/index",
              "contentSlug": "index",
              "contentAlias": "Index",
              "navigationAnchor": "Home",
              "navigationOrder": "1",
              "subNavigationItems": []
          },
          {
              "navigationMenuId": "2",
              "parentNavigationMenuId": "0",
              "fullURL": "/top-nav-1/index",
              "contentSlug": "index",
              "contentAlias": "Index",
              "navigationAnchor": "Top Nav 1",
              "navigationOrder": "2",
              "subNavigationItems": []
          },
          {
              "navigationMenuId": "3",
              "parentNavigationMenuId": "0",
              "fullURL": "/top-nav-2/index",
              "contentSlug": "index",
              "contentAlias": "Index",
              "navigationAnchor": "Top Nav 2",
              "navigationOrder": "3",
              "subNavigationItems": [
                  {
                      "navigationMenuId": "4",
                      "parentNavigationMenuId": "3",
                      "fullURL": "/top-nav-2/sub-nav-1",
                      "contentSlug": "sub-nav-1",
                      "contentAlias": "Sub Nav 1",
                      "navigationAnchor": "Sub Nav 1",
                      "navigationOrder": "1"
                  },
                  {
                      "navigationMenuId": "5",
                      "parentNavigationMenuId": "3",
                      "fullURL": "/top-nav-2/sub-nav-2",
                      "contentSlug": "sub-nav-2",
                      "contentAlias": "Sub Nav 2",
                      "navigationAnchor": "Sub Nav 2",
                      "navigationOrder": "2"
                  },
                  {
                      "navigationMenuId": "6",
                      "parentNavigationMenuId": "3",
                      "fullURL": "/top-nav-2/sub-nav-3",
                      "contentSlug": "sub-nav-3",
                      "contentAlias": "Sub Nav 3",
                      "navigationAnchor": "Sub Nav 3",
                      "navigationOrder": "3"
                  },
                  {
                      "navigationMenuId": "7",
                      "parentNavigationMenuId": "3",
                      "fullURL": "/top-nav-2/sub-nav-4",
                      "contentSlug": "sub-nav-4",
                      "contentAlias": "Sub Nav 4",
                      "navigationAnchor": "Sub Nav 4",
                      "navigationOrder": "4"
                  }
              ]
          },
          {
              "navigationMenuId": "8",
              "parentNavigationMenuId": "0",
              "fullURL": "/top-nav-3/index",
              "contentSlug": "index",
              "contentAlias": "Top Nav 3",
              "navigationAnchor": "Top Nav 3",
              "navigationOrder": "4",
              "subNavigationItems": []
          },
          {
              "navigationMenuId": "9",
              "parentNavigationMenuId": "0",
              "fullURL": "/top-nav-4/index",
              "contentSlug": "index",
              "contentAlias": "Top Nav 4",
              "navigationAnchor": "Top Nav 4",
              "navigationOrder": "5",
              "subNavigationItems": [
                  {
                      "navigationMenuId": "10",
                      "parentNavigationMenuId": "9",
                      "fullURL": "/top-nav-4/sub-nav-1",
                      "contentSlug": "sub-nav-1",
                      "contentAlias": "Sub Nav 1",
                      "navigationAnchor": "Sub Nav 1",
                      "navigationOrder": "1"
                  },
                  {
                      "navigationMenuId": "11",
                      "parentNavigationMenuId": "9",
                      "fullURL": "/top-nav-4/sub-nav-2",
                      "contentSlug": "sub-nav-2",
                      "contentAlias": "Sub Nav 2",
                      "navigationAnchor": "Sub Nav 2",
                      "navigationOrder": "2"
                  },
                  {
                      "navigationMenuId": "12",
                      "parentNavigationMenuId": "9",
                      "fullURL": "/top-nav-4/sub-nav-3",
                      "contentSlug": "sub-nav-3",
                      "contentAlias": "Sub Nav 3",
                      "navigationAnchor": "Sub Nav 3",
                      "navigationOrder": "3"
                  }
              ]
          },
          {
              "navigationMenuId": "13",
              "parentNavigationMenuId": "0",
              "fullURL": "/top-nav-5/index",
              "contentSlug": "index",
              "contentAlias": "Top Nav 5",
              "navigationAnchor": "Top Nav 5",
              "navigationOrder": "6",
              "subNavigationItems": []
          }
      ]
  </code></pre>
  
  <p>Please let me know you can think of a better way to handle this than what i am thinking.  Thanks!</p>