add leading zeros


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.