Hi Tom, I think the problem you are having is that you are assuming that the the array returned from GetDepartments is a 2 dimensional array. It isn’t. Its a multi-dimensional array like such:
Also its not a stored procedure, and you are not assigning anything to it, its an array returned from a function (or method), and you are adding other dimensions to it.
Hope that clears it up for you. multi-dimensional array can be tricky to get your head around for some people when you start learning about them.
To access a specific element you need to select a valid index. If the array has 2 elements then it would contain 0 and 1.
IE:
$array = array('Hello', 'World');
echo $array[0]; // output is Hello
echo $array[1]; // output World
By grabbing the count of the entire array you can easily access each element.
for ($i=0; $i<count($this->mDepartments); $i++)
// In reality, $i is equal to 0, then 1, 2 and so on
$this->mDepartments[$i]['link_to_departments'] = 'index.php?DepartmentId=' . $this->mDepartments[$i]['department_id'];
This says:
Loop while the variable i is less than the total elements in the array.
Assign a value to a particular element. (Read up on bi-dimensional multi-dimensional arrays)
$this , which is an Object, contains an element [‘mDepartments’]. There may be more elements of $this, but they are not used here.
mDepartments contains an unknown number (count($this->mDepartments)) of elements in an enumerated array ([$i]).
Each element of the mDepartments array mDepartments[0], mDepartments[1], etc. Contains at least two elements in an associative array: ‘link_to_department’ and ‘department_id’. There may be more elements of these arrays, but only these two are being used.
Each link_to_department and department_id contain a value.
for ($i=0; $i<count($this->mDepartments); $i++)
$this->mDepartments[$i][‘link_to_departments’]
= ‘index.php?DepartmentId=’ . $this->mDepartments[$i][‘department_id’];
[/COLOR]
The first line refers to a stored procedure that returns…
department_id name
-------------- ---------
0 Regional
1 Nature
2 Seasonal
So, mDepartments is a two-dimensional array like mDepartments[department_id][name], right?
If so, then $this->mDepartments[1] would equal “1”, right?
And, $this->mDepartments[2][0] would equal “Seasonal”, right?
It was added in the for loop. There is nothing surreptitious about it, you can add extra dimensions to arrays at any time. There are many ways in php to create an array structure and add vales to it.
Sorry, you missed some important things in my last post.
$this->mDepartments = Catalog::GetDepartments();
Catalog::GetDepartments is a stored procedure which returns…
department_id name
-------------- ---------
0 Regional
1 Nature
2 Seasonal
So what does the structure of $this->mDepartments look like after you assign it to that stored procedure?
(One thing I dislike about PHP is how nothing is defined ahead of time. It seems very sloppy to me, and it is very confusing when you are trying to figure things like this out.
It took me forever to figure out how you could assign an array to a single variable with no defined indices?!)
I understand that mDepartments is an array, and I understand accessing elements in the array, i.e. mDepartments[$], but I don’t understand the text below in bold…
These are the parts I don’t get in particular…
$this->mDepartments[$i][‘link_to_departments’]
$this->mDepartments[$i][‘department_id’];
If those are variables or additional indexes, I don’t know where they came from or what they mean?!