|
Posted by Dimitry Rybkin on 08/22/05 04:48
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.
>
[Back to original message]
|