For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > February 2007 > Re: Replacing key in associative array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Replacing key in associative array
Geoff Berrow

2007-01-30, 3:58 am

Message-ID: <6Kxvh.3172$9S5.2639@text.news.blueyonder.co.uk> from David
Smithz contained the following:

>if there is a key with value "key2", change this key name so it is instead
>"key number 2" (while maintaining the keys value).
>
>This is harder to achieve then I thought unless I'm getting myself in a
>muddle?


$array['key number 2']=$array['key2'];
//optionally
unset($array['key2']);

--
Geoff Berrow 0110001001101100010000000110
0011011010110110010001101111011001110010
11
1001100011011011110010111001110101011010
11
David Smithz

2007-01-30, 7:58 am


> Smithz asked:
>
>Geoff said:
> $array['key number 2']=$array['key2'];
> //optionally
> unset($array['key2']);


Thanks for this Geoff. Works. However does lose the ordering of my array. Is
there a way of maintaining the order as I guess the above puts in a new
element, and then just removes the old one.

How can I as well maintain the order.

thanks


Geoff Berrow

2007-01-30, 7:58 am

Message-ID: <BkFvh.196175$MO2.66573@fe3.news.blueyonder.co.uk> from
David Smithz contained the following:

>
>Thanks for this Geoff. Works. However does lose the ordering of my array. Is
>there a way of maintaining the order as I guess the above puts in a new
>element, and then just removes the old one.
>
>How can I as well maintain the order.


It's difficult to advise without knowing exactly what you want to do but
you could make a copy of the array changing the keys as necessary.

foreach($array as $key=>$value){
if($key=="key2"){
$key="key number 2";
}
$temparray[$key]=$value;
}
$array=$temparray;



--
Geoff Berrow 0110001001101100010000000110
0011011010110110010001101111011001110010
11
1001100011011011110010111001110101011010
11
Rik

2007-01-30, 7:58 am

David Smithz <dave1900@blueyonder.co.uk> wrote:

>
[color=darkred]
>
> Thanks for this Geoff. Works. However does lose the ordering of my =


> array. Is
> there a way of maintaining the order as I guess the above puts in a ne=

w
> element, and then just removes the old one.
>
> How can I as well maintain the order.


http://nl2.php.net/manual/en/function.array-splice.php

-- =

Rik Wasmus
David Smithz

2007-01-30, 6:58 pm


"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:<0r8ur2p751ajikm57gh5avfc05cahbci2e@

>


> It's difficult to advise without knowing exactly what you want to do but


> you could make a copy of the array changing the keys as necessary.


>


> foreach($array as $key=>$value){


> if($key=="key2"){


> $key="key number 2";


> }


> $temparray[$key]=$value;


> }


> $array=$temparray;




Guess that would be a way of achieving it. The details are that I have a
webform where I want text fields with a name of a certain format to get
emailed automatically.



E.g. if a input text element is called name="FIELDTOEMAILfirstname"



A php script will automatacially detect this and include the field in an
email that is sent. In this particular case the identifier part of the field
name is striped and inserted into the email is the remaining part of the
field followed by the contents. E.g.

firstname:field contents as user entered.



I have found this is a great way of adapting forms quickly and still having
the new fields automatically emailed.



However, to improve presentation in some cases, I want to be able to rename
the field name that is displayed in the email. So in the example above we
might want to replace firstname with "First Name".



If I could easily go through the array replacing the keys (As that is how
the email is generated, it just runs down an associating array printing
keyname followed by value) I thought this would add to this neat little
system.



But to be honest you are probably going to say I might as well just do the
last bit they way you have said above, but I'm open to suggestions.



Thanks


Geoff Berrow

2007-01-31, 3:58 am

Message-ID: <bARvh.187755$Kh7.174122@fe2.news.blueyonder.co.uk> from
David Smithz contained the following:

>However, to improve presentation in some cases, I want to be able to rename
>the field name that is displayed in the email. So in the example above we
>might want to replace firstname with "First Name".


What I do is create a lookup array.

$lookup=array("firstname"=>"First name", "lastname"="Last
name","addr1"=>"Address line 1", ... );

Your values will be returned in the $_POST array so basically it is
something like this

$mailbody= "Values submitted from form\n\n";
foreach($_POST as $key=>$value){
$mailbody.= $lookup[$key]." - ".$value."\n";
}

I sometimes have an exclude array too because you may not want to
display submit buttons or hidden fields.

--
Geoff Berrow 0110001001101100010000000110
0011011010110110010001101111011001110010
11
1001100011011011110010111001110101011010
11
David Smithz

2007-01-31, 7:59 am


"Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
news:1gn0s21s0iojakntf673jl6cf7e0ubbijb@
4ax.com...
>thanks Geoff,


Yes using a lookup array and using it before preparing the email to send is
a much better approach!

I added the following

$lookup[$key]== "" ? $body.= $key : $body.= $lookup[$key] ;

that way if a variable was missed out I would still show in the email (which
is what I want).

On a slightly related subject I thought I would mention the following.
I want to prevent Spammers abusing my form to email. Not sure if this
happens as there are probably easier ways of doing SPAM, but as my form has
a free text field for comments, and the person filling the form enters their
email address and are emailed a copy of what they entered, is it not
possible for a spammer to abuse this.

What checks would you put in place to try and prevent this or is it too much
hassle for Spammers to do this.

Thanks


Christoph Burschka

2007-02-01, 7:59 am

David Smithz schrieb:
> "Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
> news:<0r8ur2p751ajikm57gh5avfc05cahbci2e@
>
>
>
>
>
>
>


Actually, I think this approach might be the most elegant and still
keeping the previous order. It assumes that there is an array $replace,
which maps the current key to the one it should be replaced with.

$keys=array_keys($array); // extract keys
$values=array_values($array); // extract values
foreach ($keys as $i=>$key) $keys[$i]=$replace[$key]; // replace
$array=array_combine($keys,$values); // map back together

This is just off the top of my head and untested - it should work
according to what I read on the php.net pages, but I haven't tried it out.

array_keys and array_values maintains the current order of the array,
however, so the keys should be mapped to the same values as before.

--
CB
David Smithz

2007-02-01, 9:57 pm

Thanks for all the input on this matter.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com