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! Quote Link to comment 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>"; Quote Link to comment 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. Quote Link to comment 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>"; Quote Link to comment 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); Quote Link to comment Share on other sites More sharing options...
saeed_violinist Posted March 10, 2009 Author Share Posted March 10, 2009 Thanks, very nicely done. Quote Link to comment 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.