Jump to content

Trouble with replacing values from array


Mcod

Recommended Posts

I am having issues with replacing strings - see example below:

 

function getname($string) {     
$search = array('0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22');      
$replace = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v");     
return str_replace($search, $replace, $string); 
}

 

If I run getname(22); then it returns cc instead of v, meaning it replaces the 22 with two times the 2

 

Your help would be appreciated :)

 

Thanks

You would have to run it backwards, but then what happens when you WANT cc instead of v? Or want cb and that's 21, and you replace it with u?

You need to zerofill your numbers, or use a deliminator.

Why are you doing this?

Thank you for your reply. The reason why I need to do this is because I had a script coded and there are category numbers which I want to replace with words. There are 40 categories, so I looked up some ways to solve this, but as you can see, it causes more pain than it does good :)

 

Any recommendation how to solve this with a similar function that actually works?

 

Thanks

For me it's simple, I have a value like 22 and to me it's clear that "22" means "Cars" and "2" means "Business". So what I get from my mysql database is the value 22 which means the listing is for the category "Cars" and a result with 2 would be the category "Business". Since I have to print out the category in human-readable form instead of showing "Category: 22" I am looking for a function to convert the number 22 into the word "Cars" - right now (as you know) it would echo BusinessBusiness which is not what I want :)

... How on earth does 22 == cars and 2 = business? you said 22 = v

 

How many categories are there? You should have a table which has each category and it has an ID, which it SOUNDS like you sort of already have - and then store the category id with each product, and join the tables.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.