Jump to content

Quicker/more simpler way of doing this?


newbtophp

Recommended Posts

          $array = array();
          for ($i = 97; $i < 123; $i++) {
              $array[] = chr($i);
          }
          for ($i = 65; $i < 91; $i++) {
              $array[] = chr($i);
          }

 

Its quite time consuming looping, perhaps combine in 1 or?

Link to comment
https://forums.phpfreaks.com/topic/212775-quickermore-simpler-way-of-doing-this/
Share on other sites

Well, you can reference characters in a string the same way you reference elements in an array. So the simplest solution would be to simply define a string with all the characters

$array = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

 

However, if you need to keep it as an array construct and/or need it to be done programatically, then this might be more efficient

$array = array_merge(range(97, 122), range(65, 90));
$array = array_map("chr", $array);

 

However, there are many different possible solutions and I can't say which would be more efficient without testing.

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.