title: Need to convert piece of PHP code to C#
body: <p>I need to convert two PhP functions (sfGetCategoryAllTree and sfSideTree) to C#. I'm not used to PhP at all. I will show you the functions and what I did by now.</p>
  
  <pre><code>/**
   * Retrieves user data given a user id or user object.
   *
   * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
   * @return Category|null Category on success or null on failure
   */
  function sfGetCategoryAllTree($output = OBJECT,$id='') {
  
      global $sfdb, $table_category, $table_category_lang;
  
      $categories = $sfdb-&gt;run("SELECT * FROM $table_category c"'
          ORDER BY position ASC", '', true)
              -&gt;fetchAll($output);
  
      $all_categories = array();
      $all_relation = array();
      $tree = array();
      foreach ($categories as $c) {
  
          $all_categories[$c['id']] = $c;
          if ($c['parent_id'] == null) {
              $tree[] = $c;
              $all_relation[0][] = $c['id'];
          } else {
              $all_relation[$c['parent_id']][] = $c['id'];
          }
      }
  
      if (isset($all_relation[0])) {
          $tree = sfSideTree($all_relation[0], $all_categories, $all_relation);
      }
      return ($tree) ? $tree : array();
  }
  
  /**
   * Helps create the tree
   *
   * @param array $branch
   * @param array $categories
   * @param array $relation
   * @return array
   */
  function sfSideTree($branch, $categories, $relation) {
      $tree = array();
      if (!empty($branch)) {
          foreach ($branch as $b) {
              $aux = $categories[$b];
              if (isset($relation[$b]) &amp;&amp; is_array($relation[$b])) {
                  $aux['categories'] = sfSideTree($relation[$b], $categories, $relation);
              } else {
                  $aux['categories'] = array();
              }
              $tree[] = $aux;
          }
      }
      return $tree;
  }
  </code></pre>
  
  <p>Here what I did so far:</p>
  
  <pre><code>public void sfSideTree(int[] branch, Category[] categories, int[] relation)
          {
              Category[] tree = new Category[10];
  
              foreach (var b in branch)
              {
                  var aux = categories[b];
                  if (relation[b] != null)
                  {
                      aux[]....
                  }
              }
          }
  
          public void sfGetCategoryAllTree()
          {
              var categories = this.Query().Select().ToList();
  
              /*
              $all_categories = array();
              $all_relation = array();
              $tree = array();
              */
              //IList&lt;Category&gt; all_categories = new List&lt;Category&gt;();
              //IList&lt;Category&gt; all_relation = new List&lt;Category&gt;();
              //IList&lt;Category&gt; tree = new List&lt;Category&gt;();
              Category[] all_categories = new Category[10];
              int[] all_relation = new int[10];
              Category[] tree = new Category[10];
  
  
              foreach (var c in categories)
              {
                  //$all_categories[$c['id']] = $c;
                  all_categories[c.Id] = c;
                  //all_categories.Add(new Category() { Id = item.Id });
                  if (c.ParentId == 0)
                  {
                      tree[??] = c; //???
                      all_relation[0] = c.Id;
                      /*
  
                      $tree[] = $c;
                      $all_relation[0][] = $c['id'];*/
                  }
                  else
                  {
                      all_relation[c.ParentId] = c.Id;
                      //$all_relation[$c['parent_id']][] = $c['id'];
                  }
              }
  
              if (all_relation != null)
              {
                  sfSideTree(all_relation[0], all_categories, all_relation);
              }
  
                  return tree;
              /*return ($tree) ? $tree: array();*/
          }
  </code></pre>
  
  <p>PhP arrays make life harder to translate. Does someone can help me?</p>
  
  <p>David</p>
  
  <h2>Edit</h2>
  
  <p>To answer to OwlSolo here the structure of Category class</p>
  
  <pre><code>public class Category
      {
          public int Id { get; set; }
          public int ParentId { get; set; }
          public int Position { get; set; }
          public string Title { get; set; }
          public string Description { get; set; }
          public bool Enabled { get; set; }
      }
  </code></pre>