Re: changing a key in an array
| From: | David T-G | Date: | Thu, 23 Jan 2003 14:05:37 +0000 |
| Subject: | Re: changing a key in an array | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-132831@lists.php.net to get a copy of this message | ||
Brent, et al --
..and then Brent Baisley said...
%
% I'm not sure how you are creating your array, but your resulting array
Well, here's some code with some commentary to perhaps help:
- first we define a name for a picture basket; the default is
'default' (we might later get a new name from the user or replace
an existing basket and save the collection)
- we base64_encode the basket name (if it isn't already done) so that
we can safely store it and use it as a key in the basket list
- so we're ready to populate $pixbasket, an array of values (path to
picture) and keys (base64_encoded version of the value, in fact, for
the same sorts of reasons); we're given a base64_encoded path as the
picture to put in the basket:
if ( $fn == "add" || $fn == "rm" ) # are we adding or removing a pic?
{
...
$p = base64_decode($basketpic); # get the pic path
...
case add: $pixbasket[$basketpic] = $p; break ; # add to basket
...
}
- when it's time to save the basket, we put the whole array (as a
value) onto the basket list array:
if ( $fn == "manage" ) # still?
{
...
if ( $basketsave && ( $newname || $target ) ) # what to do and how to do it?
{
if ( $newname ) { $basketname = base64_encode($newname) ; } # use this if we have it ...
else { $basketname = $target ; } # ... or else use 'target' (already encoded)
$basketlist[$basketname] = $pixbasket ; # put it in the array
...
}
- then we write the whole basket list array to a file and get it back
some other day
% would look something like this:
%
% Array
% (
% [name1]=>namevalue1
% [a1]=>Array
% (
% [k1]=>value1
% [k2]=>value2
% )
% [name2]=>namevalue2
% [a2]=>Array
% (...
Hmmm...
%
% You end up with a two dimensional array (avoiding the columns term) that
% contains a name value and a values array for each entry.
OK. I was able to construct this directly, but I don't see how to use
it; if I ask for array_keys() for the outer array I'll get both types of
keys and that would be a mess.
I suppose I could have another array like
$names = array ('name1', 'name2', 'name3') ;
where I maintain the order (renaming 'name2' is as simple as
$names[1] = 'newname' ;
once I determine that I'm working with position 1) and then have my array
of baskets, now simply by ID instead of like a hash, so
$baskets = array (array(...),array(...),...) ;
would work) and then wrap both of those into the single array for writing
(like
$basketlist = array($names,$baskets) ;
or so), but that seems even more of a kludge :-)
Given the array you present above, how would you extract a basket by
name? The closest I can come is to go and find the name and then
increment the ID by 1 and then get out that basket, which sounds like a
*huge* mess...
Thanks again & HAND
:-D
--
David T-G * There is too much animal courage in
(play) davidtg@justpickone.org * society and not sufficient moral courage.
(work) davidtgwork@justpickone.org -- Mary Baker Eddy, "Science and Health"
http://justpickone.org/davidtg/ Shpx gur
Pbzzhavpngvbaf Qrprapl Npg!