...

View Full Version : Changing Duplicate Array Values?



warlord
03-11-2007, 09:05 AM
Hello,

I need some help trying to change duplicate array values to unique ones. Is there a way to search for all the 'unknown' values in the array and rename them to something unique?






[name] => unknown
[city] => new york
[zipcode] => unknown
[region] => east
[year] => 2007
[gender] => unknown
[experience] => unknown


I would like to rename them to something unique such as 'unknown 01' , 'unknown 02', etc. dynamically without having to know the key name ... Any help is much appreciated!

~ Thanks

zro@rtv
03-11-2007, 11:34 PM
$i=0;
foreach ($array as $key => $value){
if ($value == "unknown" ){
$array[$key] = "unknown" . $i;
$i++;
}
}


dunno if thats the most elegant, but should work.
Also, not sure how multidimensional yr array is, if its deep obviously there needs to be mod.

warlord
03-12-2007, 06:40 AM
$i=0;
foreach ($array as $key => $value){
if ($value == "unknown" ){
$array[$key] = "unknown" . $i;
$i++;
}
}


dunno if thats the most elegant, but should work.
Also, not sure how multidimensional yr array is, if its deep obviously there needs to be mod.

Hi zro,

I appreciate the response ... one question - my array is named '$output', where do I put that into the code?

Thanks ~

Fou-Lu
03-12-2007, 06:52 AM
Switch anything you see that says '$array' with '$output' no quotes of course. Unfortunatly, this isn't exactly a unique seeker, it will only replace when it finds the word 'unknown'. Does that still satisfy what you need, because if it does, this will fix it. Otherwise, we'll be looking at a recursion technique to search it...

warlord
03-12-2007, 07:34 AM
Thanks for the help! ... I really appreciate it. The code works perfectly! :thumbsup:

Regards ~



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum