0

First of all, I apologise for the title, I didn't found a good one for this problem.

I'm working on a Symfony project which already exists and I need to add something. So I'm reading it and I came to a point where arrays are used and I don't understand how it works.

$this->categories = Doctrine_Query::create()
            ->from('Category c')
            ->leftJoin('c.Sizings s')
            ->where('c.level = 0')
            ->orderBy('c.id ASC, s.gender ASC, s.size ASC')
            ->execute();

        $rCategories = Doctrine_Query::create()
            ->from('Category c');
        $allCategories = $rCategories->execute();

        $cat = array();
        foreach($allCategories as $c) {
              [...]
              $cat[$c->getRootId()]['children'][$c->getId()]['root'] = $c;
              [...]

So I made some test to see how to access values and this :

$cat[$c->getRootId()]['children'][$c->getId()]['root'] = $c;
    foreach($cat as $ca){
      print($ca['root']);
      echo '<br/>';
      foreach($ca['children'] as $child){
        print($child['root']);
      }
    }
die;

displays :

Tennis Racket //This is the main category
Thunder       //And this is a subcategory

First of all, why isn't it IDs instead of Names?

What does this code means exactly?

$cat[$c->getRootId()]['children'][$c->getId()]['root'] = $c;

At the end there is this :

$this->allCategories = $cat;

I will use $allCategories in a partial in order to display each categories and its subcategories and then include it in my indexSuccess.

But here is the final point, I have to had a new column. The column is already in the DB and I can access its value in the actionclass with

$c->getBrand();

I would like to had it in the array cat and then in $this->allCategories so I can access it in the partial which I use in the indexSuccess and finally set values in the column brand that I had had in the partial.

I'm afraid that I'm not clear enough but in a nutshell : I don't understand this line :

$cat[$c->getRootId()]['children'][$c->getId()]['root'] = $c;

And I would like to had a new parameter in the array $cat and access this new value after giving it to indexsuccess through

$this->allCategories = $cat;

And I want to know how to access the new value.

I hope I'm clear enough, Thank you for taking the time to read this.

MrPanda
  • 99
  • 1
  • 9
  • I propose you `var_dump` or `print_r` the `$cat` variable to see its structure. This might help analysing the code. – Voitcus May 16 '13 at 10:25
  • Would you understand `$cat['foo']['children']['bar']['root'] = $c;`? Or are multi-dimensional associative arrays a mystery to you overall? – deceze May 16 '13 at 10:30
  • var_dump or print_r gives way too much lines.. @deceze Nope, it's this that I don't understand. I don't know a lot about multi-dimensional arrays. But the only thing I want is to had a third value to $cat which is $c->getBrand() – MrPanda May 16 '13 at 11:58
  • Possible duplicate of [Array parts access](http://stackoverflow.com/a/16467994/476). Read that for an introduction to arrays. – deceze May 16 '13 at 12:05

1 Answers1

0

First of all, since you don't include any code for $c, it's hard to understand what it actually contains. But in a nutshell, $c is an instance of some sort of class.

Most probably it came into existence this way:

$c = new someSortOfClass;

If you checked out the code for someSortOfClass, you'd find getRootId() and getId() as methods inside it's definition. What they do, is up to the code, and since there is no code to look up to - I assume they return some sort of value that can be a key or index to an array.

What happens next - new elements are created inside a multi-dimensional array and the whole instance $c is assigned to the 4th level 'root' array key:

$cat[$c->getRootId()]['children'][$c->getId()]['root'] = $c;

If you made a var_dump($cat), you would see the entire tree for this array and could then appreciate how it's multi-dimensionality works.

In essence - php arrays can have any sort of value, that include other arrays, so this:

$array = Array('key1': 'string', 'key2': Array('child_key1': 2));
print $array['key2']['child_key1']; // 2

Is an example of such an array.

Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
  • I just edited my post and included the code which leads to the var $c. – MrPanda May 16 '13 at 11:40
  • Indeed, `getRootId` and `getRoot` are in the Category class. Symfony creates all `get[column]` for each column in each tables of the database. Here it creates all `get` for each columns of the table Category. So `getRootId` returns the value of the column "rootId" which is an `int` That's why I don't understand why it displays the string related to the ID and not the ID itself. – MrPanda May 16 '13 at 11:46
  • So what I guess `Doctrine_Query` does is abstract a database connection. In that case, whatever it returns - is **always** a string. You have to do type conversion on the returned values yourself. @MrPanda – Morgan Wilde May 16 '13 at 12:02