note 24386 added to language.variables.variable
| From: | (J. Dyer) | Date: | Wed, 14 Aug 2002 21:08:49 +0000 |
| Subject: | note 24386 added to language.variables.variable | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-35032@lists.php.net to get a copy of this message | ||
A final note,
I have not seen this anywhere else, and I am unsure exactly where to post this snippet. This
is another example from the same program I used in the previous entries.
Essentially, I wanted to make a dynamically generated, multi-dimensional, associative array.
The dynamic generation consists of an association within the array of a certain key attribute within
those variably-named objects from my previous example.
Yes, there are other, easier ways to associate objects than this manner; however, for this
particular project, the snippet is a solution for a requested "feature" on a website.
That feature consists of displaying the values from only certain, user-specified objects by their
"key" value.
[note: the name of the variables/arrays change from example to example to avoid ambiguity within the
script. The $obj_array is the same as the previous $array. Also, there are two different methods
to place the $obj object into the array, and this was a trial to see which one worked. They both
do.]
<?php
// ...
foreach ($this->obj_array as $key=>$obj){
// if the key already exists..
if ( array_key_exists($obj->GetKeyCode(), $this->new_array) ){
$key = $obj->GetKeyCode();
array_push($this->new_array[$key], $obj);
} // end if
// else if the key doesn't exist, create it.
else{
$key = $obj->GetKeyCode();
$this->new_array[$key] = array();
$this->new_array[$key][] = $obj;
} // end else
} // end foreach
//...
?>
--
http://www.php.net/manual/en/language.variables.variable.php
http://master.php.net/manage/user-notes.php?action=edit+24386
http://master.php.net/manage/user-notes.php?action=delete+24386
http://master.php.net/manage/user-notes.php?action=reject+24386