Page 1 of 1

change value in an array while processing....

Posted: Wed Jun 08, 2005 7:44 am
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]

Posted: Wed Jun 08, 2005 7:55 am
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.

Posted: Wed Jun 08, 2005 8:04 am
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......

Posted: Wed Jun 08, 2005 8:15 am
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)

Posted: Wed Jun 08, 2005 8:22 am
by LostOne
Syranide, thanks, do I feel stupid. I,m sure it will not be the last time...... :roll: