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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

... 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.