note 24386 deleted from language.variables.variable by danbrown
| From: | danbrown@php.net | Date: | Wed, 24 Jun 2009 17:00:59 +0000 |
| Subject: | note 24386 deleted from language.variables.variable by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157416@lists.php.net to get a copy of this message | ||
Note Submitter: (J. Dyer) jkdyer_at_vlsmaps_com
----
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
//...
?>