title: PHP While Loop Resolve
body: <p>I am having a bit of trouble resolving things in a PHP while loop. Basically I have an array in this form (easier to represent in JSON for brevity)</p>
  
  <pre><code>{
      "node2": {
          "rowid": "2",
          "label": "Eco-Lights - Compact Fluorescent Lamps (CFL)",
          "slug": "eco-lights-compact-fluorescent-lamps-cfl",
          "prefix": "/categories",
          "parent": "55",
          "path": null,
          "weight": "100",
          "featured": "0",
          "active": "0"
      },
      "node3": {
          "rowid": "3",
          "label": "Light Movers, Hangers and Accessories",
          "slug": "light-movers-hangers-and-accessories",
          "prefix": "/categories",
          "parent": "59",
          "path": null,
          "weight": "100",
          "featured": "0",
          "active": "0"
      }
  }
  </code></pre>
  
  <p>This array is about 150 elements give or take a few. The key of the node is simply "node"+rowid for ease of lookup (in the next part)</p>
  
  <p>What I am trying to do is take any node and go right up until its parent is zero (i/e has no parents) and on each iteration grab the label and slug of the parent element.</p>
  
  <p>So far I have done this using a while loop as below. <code>$this-&gt;categories</code> is the array as above. The trouble is that the loop is incorrect and it's hitting 4gb of memory and doing around 5,000 loops for an array of 150 elements with roughly one parent each so it should be something less than 500 iterations.</p>
  
  <pre><code>public function ResolveCategoryUrl($id) {
      $element=$element=$this-&gt;categories['node'.$id];
      $parent=$element['parent'];
      while(1) {
          $prev=$element;
          $element=$this-&gt;categories['node'.$element['parent']];
          $parts[]=$element['slug'];
          $breadcrumbs[]=['label'=&gt;$element['label'],'url'=&gt;$element['prefix'].$element['path'].$element['data']['url_postfix']];
          $parent=$element['parent'];
          if($parent&lt;=1) {
              break;
          }
      }
      return [
          'path'=&gt;array_reverse($parts),
          'breadcrumbs'=&gt;$breadcrumbs
      ];
  }
  </code></pre>
  
  <p>I cannot figure out how to tell PHP to take an element and work backward up to its parent until <code>parent = 0</code> then return what I want from the function.</p>
  
  <p>Sorry if the explanation is difficult, I have no other way to explain it!</p>