s4surbhi2218 Posted December 2, 2012 Share Posted December 2, 2012 Hi, I want to code such that it represents numbers from 0-9, a-z, A-Z and then from 10-19,1a-1z,1A-1Z and so on example: no change for numbers from 0-9 numbers 10,11,12......upto 36 are represented by alphabets a,b,c.....upto z numbers 37,38,39......upto 62 are represented by alphabets A,B,C.....upto Z numbers 63 , 64 ...upto 73 are repreneted by 10,11,12,13 and so on upto 19 from 74 ,,,,again 1a follows Whats the best/wisest way to do that?Any suggestions would be great. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/ Share on other sites More sharing options...
Christian F. Posted December 2, 2012 Share Posted December 2, 2012 (edited) Create an array using range () and array_merge (), and match the number with the position (index). Voilá, you got the correct "letter". Alternatively, if that array never (or almost never) changes you could print out the result. Then replace the generation code with the actual array. Shouldn't be necessary though, unless you're having a high load, and the benchmark shows that said code is taking up a lot of the time. Edited December 2, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1396971 Share on other sites More sharing options...
s4surbhi2218 Posted December 3, 2012 Author Share Posted December 3, 2012 Create an array using range () and array_merge (), and match the number with the position (index). Voilá, you got the correct "letter". Alternatively, if that array never (or almost never) changes you could print out the result. Then replace the generation code with the actual array. Shouldn't be necessary though, unless you're having a high load, and the benchmark shows that said code is taking up a lot of the time. did this <?php $n = 35;//example $arrSmall = array(); $arrSmallNum = array(); $arrSmall = range('a','z'); print_r($arrSmall); $arrSmallNum = range('10','35'); print_r($arrSmallNum); $index = array_search($n,$arrSmallNum);echo $index; echo "-------".$arrSmall[$index]."------"; ?> But in this i would need to create an array again an again moreover how would i create an array for range('1a','1z')?? Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397068 Share on other sites More sharing options...
Christian F. Posted December 3, 2012 Share Posted December 3, 2012 (edited) You seem to have missed most of my post, in particular the second paragraph. As for generating an array with numbers and letters, this is one way to do it: $l = array (); $r=0; for ($c = ord ('a'); $c <= ord ('z'); $c++) { $l[++$r] = '1'.chr ($c); } PS: No need to use array_search () either. Not only won't it do what you've described, but isset ($arr[$index]) does the job a lot easier than array_key_exists (). Edited December 3, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397085 Share on other sites More sharing options...
s4surbhi2218 Posted December 3, 2012 Author Share Posted December 3, 2012 did this <?php $n = 35;//example $arrSmall = array(); $arrSmallNum = array(); $arrSmall = range('a','z'); print_r($arrSmall); $arrSmallNum = range('10','35'); print_r($arrSmallNum); $index = array_search($n,$arrSmallNum);echo $index; echo "-------".$arrSmall[$index]."------"; ?> But in this i would need to create an array again an again because i want it for upto 1000 approx moreover how would i create an array for range('1a','1z')?? Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397086 Share on other sites More sharing options...
s4surbhi2218 Posted December 3, 2012 Author Share Posted December 3, 2012 (edited) You seem to have missed most of my post, in particular the second paragraph. As for generating an array with numbers and letters, this is one way to do it: $l = array (); $r=0; for ($c = ord ('a'); $c <= ord ('z'); $c++) { $l[++$r] = '1'.chr ($c); } PS: No need to use array_search () either. Not only won't it do what you've described, but isset ($arr[$index]) does the job a lot easier than array_key_exists (). I can see your point , also the snippet is able to generate one of the required arrays but my problem is that i have to represent numbers in different range differently ,as number in the range 10, 35 would be represented by alphabets in range a-z for this i would have to generate many such arrays moreover i want a representation for 1000 numbers approx . Please correct me if i am getting you wrong. Edited December 3, 2012 by s4surbhi2218 Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397091 Share on other sites More sharing options...
Christian F. Posted December 3, 2012 Share Posted December 3, 2012 (edited) In case you didn't understand my hint, in other words the first paragraph of my last post: I have corrected you. You need to re-read my first post. I've already covered all of your concerns. Only, in this case I think you can safely forgo the benchmark, since your array is as large as it is. Edited December 3, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397093 Share on other sites More sharing options...
Zane Posted December 3, 2012 Share Posted December 3, 2012 Maybe I'm just reading your question wrong or maybe it is just too early in the morning, but I cannot seem to grasp the logic of what you want to accomplish. From what I understand, the letter a would represent 10? The number 36 would be represented by z? Afterthat I get completely lost? What exactly would 3A represent? or 1A, 1G, etc.. It would help if you could elaborate more on what certain numbers would be represented by. Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397105 Share on other sites More sharing options...
salathe Posted December 3, 2012 Share Posted December 3, 2012 (edited) Whats the best/wisest way to do that? Any suggestions would be great. You are essentially looking to change the numbers from base 10 to base 62. PHP already has tools available to do this work for you, in the GMP extension. A basic example would look like: echo gmp_strval(gmp_init(12345, 10), 62); // Outputs: 3D7 Note that the above will use a slightly different map of characters than you mentioned in the first post (A-Z comes before a-z). If you want to keep your specified letter order then you could use something like strtr() to convert upper- to lower-case and vice versa. P.S. Your math in the first post is a little off at the boundaries, for example decimal 35 (not 36) should be z. Edited December 3, 2012 by salathe Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397106 Share on other sites More sharing options...
Christian F. Posted December 3, 2012 Share Posted December 3, 2012 (edited) He wants the number to be the index of an array, which returns the desired letter, number or combination of such. Since the first ten items in the array is the numbers 0-9, the 10th index would give the lowercase letter "a". (Also since 26+10=36 index 36 would give the letter 'z', or at least if we account for the goalpost issue in the OPs description. ) That said, I do agree that the logic is really convoluted, and I'm almost positive that there has to be a better way to do what the OP is trying to do. Whatever that is. I cannot see any need for a 1000+ item array doing this kind of substitution. Edited December 3, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397107 Share on other sites More sharing options...
salathe Posted December 3, 2012 Share Posted December 3, 2012 (edited) He wants the number to be the index of an array, which returns the desired letter, number or combination of such. Does she? You suggested an array, not the OP. Edited December 3, 2012 by salathe Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397124 Share on other sites More sharing options...
Christian F. Posted December 3, 2012 Share Posted December 3, 2012 True, I did. After your post, I think you might be onto something. And if that what s/he wants to accomplish, then I agree that using GMP would be the best solution. Other than that, a static array with the key-value associations is probably the "best" method for something like this. Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397140 Share on other sites More sharing options...
s4surbhi2218 Posted December 4, 2012 Author Share Posted December 4, 2012 (edited) You are essentially looking to change the numbers from base 10 to base 62. PHP already has tools available to do this work for you, in the GMP extension. A basic example would look like: echo gmp_strval(gmp_init(12345, 10), 62); // Outputs: 3D7 Note that the above will use a slightly different map of characters than you mentioned in the first post (A-Z comes before a-z). If you want to keep your specified letter order then you could use something like strtr() to convert upper- to lower-case and vice versa. P.S. Your math in the first post is a little off at the boundaries, for example decimal 35 (not 36) should be z. Yes i agree, it was really great knowing this. Edited December 4, 2012 by s4surbhi2218 Quote Link to comment https://forums.phpfreaks.com/topic/271489-represent-numbers-as-desired-alphabets/#findComment-1397410 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.