Posted By

DADU on 08/13/10


Tagged

list numbers array zero leading integers


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

DADU
roli000
mgerdt
guillermoj88gmailcom


add leading zeros


 / Published in: PHP
 

This PHP code snippet checks each integer in an array. If necessary, one or more leading zeros are added to an integer. Note that each integer becomes a string.

  1. /* EXAMPLE
  2.  
  3. print_r(add_leading_zeros(range(1,100)));
  4.  
  5. */
  6.  
  7. function add_leading_zeros($array) {
  8.  
  9. foreach($array as $key=>$value) $array[$key] = str_pad($value,strlen(max($array)),'0',STR_PAD_LEFT);
  10.  
  11. return $array;
  12. }

Report this snippet  

You need to login to post a comment.