435,165 Members | 894 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

 P: n/a Hi all, Here's a puzzle: // Say I have an array of numbers set up like so: \$arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so: addleadingzeros_arr(\$arr); // and have the output look like: // array("0015","0016","0017","0100","0121","1000"); Or, if the function only does one value like shown, so I can loop the array through, modifying each seperately. addleadingzeros_int(32); // Outputs: "0032" I'd prefer the first one though. Thanks, TheTeapot Aug 11 '05 #1
8 Replies

 P: n/a On 11 Aug 2005 00:21:52 -0700, TheTeapot wrote: \$arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so: addleadingzeros_arr(\$arr); // and have the output look like: // array("0015","0016","0017","0100","0121","1000"); http://php.net/sprintf http://php.net/foreach function f( \$a ) { \$b = array(); foreach ( \$a as \$v ) \$b[] = sprintf( '%04d', \$v ); return \$b; } Or using http://php.net/array-map function f( \$i ) { return sprintf( '%04d', \$i ); } \$b = array_map( 'f', \$a ); Or your own implementation of sprintf using http://php.net/substr \$padded = substr( '000'.\$int, -4 ); -- E. Dronkert Aug 11 '05 #2

 P: n/a TheTeapot schrieb: Hi all, Here's a puzzle: // Say I have an array of numbers set up like so: \$arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so: addleadingzeros_arr(\$arr); You can create every function ;-) function addleadingzeros_arr(\$arr) { } // and have the output look like: output? you're talking of the return value ... // array("0015","0016","0017","0100","0121","1000"); loop through \$arr; eg. foreach(\$arr as \$value) { use sprintf } Or, if the function only does one value like shown, so I can loop the array through, modifying each seperately. addleadingzeros_int(32); // Outputs: "0032" sprintf is what you ar looking for I'd prefer the first one though. Hope it help Jo Aug 11 '05 #3

 P: n/a TheTeapot wrote: \$arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so: addleadingzeros_arr(\$arr); // and have the output look like: // array("0015","0016","0017","0100","0121","1000"); \$arr = array(15,16,17,100,121,1000); addleadingzeros_arr(\$arr); function addleadingzeros_arr(&\$arr) { foreach (\$arr as &\$val) \$val=substr("000\$val",-4); } Csaba Gabor from Vienna Aug 11 '05 #4

 P: n/a TheTeapot wrote: Hi all, Here's a puzzle: // Say I have an array of numbers set up like so: \$arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so: addleadingzeros_arr(\$arr); // and have the output look like: // array("0015","0016","0017","0100","0121","1000"); Or, if the function only does one value like shown, so I can loop the array through, modifying each seperately. addleadingzeros_int(32); // Outputs: "0032" I'd prefer the first one though. Thanks, TheTeapot function addleadingzeros_arr(\$arr) { \$res = array(); foreach(\$arr as \$x) if(abs(\$x)>\$maxx) \$maxx = abs(\$x); mult = 1; len = 0; while(mult \$x) \$res[\$k] = \$x<0?"-":"" . right(mult+abs(x),len); return \$res; } Will do the trick without all the overhead of sprintf. If you know how many leading zeroes you want, the first two loops can be omitted and the correct values (10000 and 4 for instance, in your specified case). The conditional and the abs can be omitted if you'll always have non negative numbers in \$arr. The code can be simplified if \$arr is subscripted by 0-n. Untested, off the top of my head. Try before committing real data. YMMV. Aug 12 '05 #6

 P: n/a Thanks all, problem solved, used a variation on Ewoud Dronkert's suggestion. Thanks for the help, TheTeapot Aug 12 '05 #7

 P: n/a TheTeapot wrote: Hi all, Here's a puzzle: // Say I have an array of numbers set up like so: \$arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so: addleadingzeros_arr(\$arr); // and have the output look like: // array("0015","0016","0017","0100","0121","1000"); Or, if the function only does one value like shown, so I can loop the array through, modifying each seperately. addleadingzeros_int(32); // Outputs: "0032" I'd prefer the first one though. Thanks, TheTeapot One thing to think about. "0121" as an integer is equivalent to 81. Numbers with leading zeros are octal. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. js*******@attglobal.net ================== Aug 22 '05 #8

 P: n/a Not if you just append two zeros in "string" form to them. So what you need to do in your function is: 1) Count how many characters the number is. 2) Depending on the above append a certain number of zeros to the front: if(\$digitCount == 2) "00" . "23" 3) Put that into a for each loop. foreach(\$arr as \$value) { if(strlen(trim(\$value)) == 2) "00" . "23"; } Or even better use a case statement inside the for each. Hope that helps, D Jerry Stuckle wrote: TheTeapot wrote: Hi all, Here's a puzzle: // Say I have an array of numbers set up like so: \$arr = array(15,16,17,100,121,1000); // How can I create a function so that I can use it like so: addleadingzeros_arr(\$arr); // and have the output look like: // array("0015","0016","0017","0100","0121","1000"); Or, if the function only does one value like shown, so I can loop the array through, modifying each seperately. addleadingzeros_int(32); // Outputs: "0032" I'd prefer the first one though. Thanks, TheTeapot One thing to think about. "0121" as an integer is equivalent to 81. Numbers with leading zeros are octal. Aug 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion. 