change value in an array while processing....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LostOne
Forum Newbie
Posts: 18
Joined: Wed Jul 28, 2004 3:21 pm
Location: Florida

change value in an array while processing....

Post by LostOne »

I want to change the value of the array items to the formated values. sorry for the dumb question, I NEVER use non numerically indexed arrays.

Code: Select all

$account = array("Id"=>200001111, "HomePhone"=>'904-654-6804');

reset($account);
while(list($key, $value) = each($account))
	{
	if($key == "HomePhone")
		{
		$value = format__number($value);//this is where i want to change the value to the formated one
		}
	else
		$value = format_sting($value);//this is where i want to change the value to the formated one
	}
Thanks........

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color][/size]
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

depends slightly on how you want it formated? either do the formating right there or use a function call that returns the value in the format you want.
LostOne
Forum Newbie
Posts: 18
Joined: Wed Jul 28, 2004 3:21 pm
Location: Florida

Post by LostOne »

what i mean is; i want $value to contain the new formated information. If i run the above: $value maintains the OLD data, my function returns the formated data. I just dont know how to put it into the current value and have it stay there for the next iteration of the array......
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

$account[$key] = $value;
will assign that key in the account-array to the new value

however, as a reminder, I recommend you to use the: (which is exactly the same as yours, with reset, list, each and so on.)

Code: Select all

foreach( $account as $key => $value ) {
    //...
}
which is the same but only faster, and a little easier to read.

EDIT: a possibility is to use array_map, which would frankly do the same thing, but can be a real eyecandy in some scripts. (the value is passed by reference)
LostOne
Forum Newbie
Posts: 18
Joined: Wed Jul 28, 2004 3:21 pm
Location: Florida

Post by LostOne »

Syranide, thanks, do I feel stupid. I,m sure it will not be the last time...... :roll:
Post Reply