saeed_violinist Posted March 9, 2009 Share Posted March 9, 2009 Dear friends, It's not just the way of coding but I want to now how can we make an array of "n" digits, for example if "n" equals "3" then we should have an array of 100 to 999, and for "n" equal 2 we should have array of 10 to 99. Any help or ideas how to do this will be appriciated! Link to comment https://forums.phpfreaks.com/topic/148603-making-an-array-of-n-digits/ Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 $n = 3; switch($n) { case 2: $array = range(10,99); break; case 3: $array = range(100,999); break; default: $array = array(); break; } print "<pre>".$array."</pre>"; Link to comment https://forums.phpfreaks.com/topic/148603-making-an-array-of-n-digits/#findComment-780424 Share on other sites More sharing options...
saeed_violinist Posted March 9, 2009 Author Share Posted March 9, 2009 Thanks, it seems to be a good idea, But actually I want to do it with any value for "n". maybe user enter 10 as "n" so we need to have array of 1000000000 to 9999999999. like this, we need to have a large switch statement, I am looking for a gerenarl way or formula for this. Link to comment https://forums.phpfreaks.com/topic/148603-making-an-array-of-n-digits/#findComment-780429 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 Ah. Here you go $n = 3; $x = str_pad(1, $n, 0); $y = str_pad(9, $n, 9); $array = range($x, $y); print "<pre>"; print_r($array); print "</pre>"; Link to comment https://forums.phpfreaks.com/topic/148603-making-an-array-of-n-digits/#findComment-780435 Share on other sites More sharing options...
Mchl Posted March 9, 2009 Share Posted March 9, 2009 $n = 10; $low = pow(10,$n); $high = pow(10,$n+1)-1; $array = range($low,$high); Link to comment https://forums.phpfreaks.com/topic/148603-making-an-array-of-n-digits/#findComment-780522 Share on other sites More sharing options...
saeed_violinist Posted March 10, 2009 Author Share Posted March 10, 2009 Thanks, very nicely done. Link to comment https://forums.phpfreaks.com/topic/148603-making-an-array-of-n-digits/#findComment-780799 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.