What does this code do? PHP Arrays?

Below is some code from a not-so-well-written book on PHP that I’m reading…

for ($i=0; $i<count($this->mDepartments); $i++)
		= 'index.php?DepartmentId=' . $this->mDepartments[$i]['department_id'];

I believe this is all PHP code (and not Smarty code).

These are the parts I don’t get in particular…




The book uses department_id and link_to_department in one place, but shows department_id and name in another place.

I get it. You’re confused about how the books gets link_to_department when it uses name at some other place.

The only reasonable way to answer that is for someone else to look through the book to find some more clues to help answer this.

Give us the name of the book, and most likely, many of us will have that book beside us, so that we can dig deeper into this to answer this for you.

Okay, but the original table that was sucked up into the object had a “department_id” field/column and a “name” field/column.

So where did “link_to_department” come from?

Did the author surreptitiously add that to the array after the fact?

That really is what this whole long, convoluted thread is about. :slight_smile:

(Combined with the fact that assigning a multi-dimensional array to an UN-defined variable, “mDepartments” bugs me to no end!!!)


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:

$this->mDepartments = array(
‘0’ => array(‘department_id’=>‘0’, ‘name’=>‘Regional’),
‘1’ => array(‘department_id’=>‘1’, ‘name’=>‘Nature’),
‘2’ => array(‘department_id’=>‘2’, ‘name’=>‘Seasonal’),


$this->mDepartments = array(
‘0’ => array(‘department_id’=>‘0’, ‘name’=>‘Regional’, ‘link_to_department’=> ‘whatever…’),
‘1’ => array(‘department_id’=>‘1’, ‘name’=>‘Nature’, ‘link_to_department’=> ‘whatever…’),
‘2’ => array(‘department_id’=>‘2’, ‘name’=>‘Seasonal’, ‘link_to_department’=> ‘whatever…’),

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.

I’m just reading a book and don’t have any of this on my computer.


How is that possible when the results from the query mentioned above are being assigned to mDepartments?

Catalog::GetDepartments() does not produce the values ‘department_id’, ‘link_to_departments’, …


mDepartments is an array.

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.


$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:

  1. Loop while the variable i is less than the total elements in the array.
  2. Assign a value to a particular element. (Read up on bi-dimensional multi-dimensional arrays)

for ($department_id = 0; $i < count($this->mDepartments); $department_id++)
= 'index.php?DepartmentId=' . $this->mDepartments[$department_id]['department_id'];

Does that make you see the answer better?

You have another array that has an element named “department_id”

There are 4 layers to this entity.

$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.

Can you please let us know the name of this book, so that we can investigate further.

So are you saying that the author added a new “column” of data to the original table??

Because there was no column called “link_to_department” in the original table.

There was just “department_id” and “name”.


Let me try again explaining where I’m confused…

$this->mDepartments = Catalog::GetDepartments();

for ($i=0; $i<count($this->mDepartments); $i++)
= ‘index.php?DepartmentId=’ . $this->mDepartments[$i][‘department_id’];
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?

So in the author’s code…

= ‘index.php?DepartmentId=’ . $this->mDepartments[$i][‘department_id’];

It looks like he is adding in another “dimension” to the array on-the-fly?

And I thought the array would be mDepartments[department_id][name], so why is it “[$i]” and then “[‘department_id’]”??


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.


$mDepartments[3] = array('department_id' => '3');
$mDepartments[3]['name'] = 'Head Office';

$mDepartments would now have another value of 3, Head Office.

Well in that case have a look at the mysql_fetch_array command.

You will find that it returns the database info as an associative array, a numeric array, or both, depending on the result type that is specified.

When it contains the associative array information, one of the rows would look like this:

array('department_id' => 0, 'name' => 'Regional')

Okay, thanks all.


They are keys for the multi-dimensional array.

$this->mDepartments = array(
0 => array('department_id' => 1, 'link_to_departments' => 'www.google.com'),
1 => array('department_id' => 2, 'link_to_departments'  => 'www.example.com')

After this line: $this->mDepartments = Catalog::GetDepartments();



The second dimension of the array is using names for each rather than numbers.

The first dimension has elements 0,1,2…

The second dimension has elements ‘department_id’, ‘link_to_departments’, …

It is called an associative array.

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…



If those are variables or additional indexes, I don’t know where they came from or what they mean?!