Re: programming question
| From: | Rasmus Lerdorf | Date: | Mon, 19 Jun 2000 16:26:29 +0000 |
| Subject: | Re: programming question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-2211@lists.php.net to get a copy of this message | ||
> Is there a way to associate an ID
> number with two separate arrays? Where can I store this info? I haven't
> started the project with oop in mind, but maybe that would be the way to go
> with this?
$prof[1] = array(1,2,3,4,5);
$prof[2] = array(6,7,8,9,0);
echo "Professor $id's array looks like this:<br>\n";
while(list(,$val)=each($prof[$id])) {
echo "$val,";
}
The above creates two simple arrays and assigns then to $prof[1] and
$prof[2]. So, what you essentially have is a two-dimensional array that
looks like this:
0 1 2 3 4
+-----------
1 | 1 2 3 4 5
|
2 | 6 7 8 9 0
To dereference this array, you can simple loop through each of $prof[1]
and $prof[2] as I showed above, or you can get directly to an array
element using something like: $prof[1][3] which could be '4' or
$prof[2][4] which would be '0'
You can also name your rows and columns directly. ie.
$prof['Smith'] =
array('Morning'=>'abc','Afternoon'=>'def');
$prof['Jones'] =
array('Morning'=>'ghi','Afternoon'=>'jkl');
This would create a 2-dimensional array that looks like this:
Morning Afternoon
Smith abc def
Jones ghi jkl
And you can of course access it like this:
$prof['Smith']['Afternoon'] which would be 'def'
There you go, a quick lesson in multi-dimensional arrays. You should be
able to massage this concept to do what you need.
-Rasmus