views:

70

answers:

2

What do

$categories[$id] = array('name' => $name, 'children' => array());

and

$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);

mean?

Thanks a lot.

+1  A: 

How should i format the output so i can learn the results that was returned?

  1. You can format your code into tables by looping on the array using for or foreach. Read the docs for each if you don't have a grasp on looping.

2.What does

$categories[$id] = array('name' => $name, 'children' => array());

and

$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);

The first line assigns an associative array to another element of the $categories array. For instance if you wanted the name of the category with ID of 6 it would look like this:

$categories[6]['name']

The second line does something similar, except when you are working with an array in PHP, you can use the [] operator to automatically add another element to the array with the next available index.

What is the uses of .= ?

This is the concatenation assignment operator. The following two statements are equal:

$string1 .= $string2
$string1 = $string1 . $string2
snicker
A: 

These all have to do with nesting arrays. first example:

$categories[$id] = array('name' => $name, 'children' => array());

$categories is an array, and you are setting the key id to contain another array, which contains name and another array. you could accomplish something similar with this:

$categories = array(
   $id => array(
      'name' => $name,
      'children' => array()
   )
)

The second one is setting the children array from the first example. when you have arrays inside of arrays, you can use multiple indexes. It is then setting an ID and Name in that array. here is a another way to look at example #2:

   $categories = array(
       $parentID => array(
           'children' => array(
                'id' = $id,
                'name' => $name
            )
       )
   )

note: my two ways of rewriting are functionally identical to what you posted, I'm just hoping this makes it easier to visualize what's going on.

GSto