note 12126 modified in function.key by felipe
| From: | felipe@php.net | Date: | Tue, 22 Jul 2008 19:03:51 +0000 |
| Subject: | note 12126 modified in function.key by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142545@lists.php.net to get a copy of this message | ||
A Method of Multi-Dimensionalizing an Associative Array (This idea was designed with Graphics (image
filenames in mind):
(A quick overview):
While working on a rather large web application, I found that I needed a way to obtain
"all" the details on images used by the application, these images (by filename) were
stored in the configuration file, in the form of associative arrays.
The following represents an example of how I tool those and converted them into multi-dimensional
associative arrays.
<?php
do {
$size = getImageSize($path.$myarray[key($myarray)]);
if ($size[2] == '1') { $size[2] = 'gif'; }
else if ($size[2] == '2') { $size[2] = 'jpg'; }
else if ($size[2] == '3') { $size[2] = 'png'; }
else if ($size[2] == '4') { $size[2] = 'swf'; }
else { $size[2] = 'UNKNOWN'; }
$myarray[key($myarray)] = Array($myarray[Key($myarray)],$size[0],$size[1],$size[2],$size[3]);
} while (next($myarray));
?>
The end result is an multi-dim. associative array which contains the values:
<?php
$myarray["this"][0] = # filename.gif
$myarray["this"][1] = # width
$myarray["this"][2] = # height
$myarray["this"][3] = # type (converted to the file extension.)
$myarray["this"][4] = # height=x width=x
?>
This may not be all that impressive to some, but it turned out to be very useful for me, so I
thought I'd share, in addition, I think it gives a "very" good example of "a
use" for the Key() function.
Frankly, I was quite happy to discover this function, I can't count the number of times I
"needed to use the key as a value".
I hope you find this code useful.
--was--
A Method of Multi-Dimensionalizing an Associative Array (This idea was designed with Graphics (image
filenames in mind):
(A quick overview):
While working on a rather large web application, I found that I needed a way to obtain
"all" the details on images used by the application, these images (by filename) were
stored in the configuration file, in the form of associative arrays.
The following represents an example of how I tool those and converted them into multi-dimensional
associative arrays.
<code sample begin>
do {
$size = getImageSize($path.$myarray[key($myarray)]);
if ($size[2] == '1') { $size[2] = 'gif'; }
else if ($size[2] == '2') { $size[2] = 'jpg'; }
else if ($size[2] == '3') { $size[2] = 'png'; }
else if ($size[2] == '4') { $size[2] = 'swf'; }
else { $size[2] = 'UNKNOWN'; }
$myarray[key($myarray)] = Array($myarray[Key($myarray)],$size[0],$size[1],$size[2],$size[3]);
} while (next($myarray));
</end code sample>
The end result is an multi-dim. associative array which contains the values:
$myarray["this"][0] = filename.gif
$myarray["this"][1] = width
$myarray["this"][2] = height
$myarray["this"][3] = type (converted to the file extension.)
$myarray["this"][4] = height=x width=x
This may not be all that impressive to some, but it turned out to be very useful for me, so I
thought I'd share, in addition, I think it gives a "very" good example of "a
use" for the Key() function.
Frankly, I was quite happy to discover this function, I can't count the number of times I
"needed to use the key as a value".
I hope you find this code useful.
http://php.net/manual/en/function.key.php