Return to Snippet

Revision: 30397
at September 4, 2010 09:22 by DADU


Updated Code
/* EXAMPLE

print_r(add_leading_zeros(range(1,100)));

*/

function add_leading_zeros($array) {
	
	foreach($array as $key=>$value) $array[$key] = str_pad($value,strlen(max($array)),'0',STR_PAD_LEFT);
	
	return $array;
}

Revision: 30396
at August 13, 2010 05:31 by DADU


Updated Code
/* EXAMPLE

print_r(addleadingzeros(range(1,100)));

*/

function addleadingzeros($array) {
	
	foreach($array as $key=>$value) $array[$key] = str_pad($value,strlen(max($array)),'0',STR_PAD_LEFT);
	
	return $array;
}

Revision: 30395
at August 13, 2010 05:29 by DADU


Initial Code
/* EXAMPLE

print_r(addleadingzeros(range(1,100)));

*/

function addleadingzeros($array) {
	
foreach($array as $key=>$value) $array[$key] = str_pad($value,strlen(max($array)),'0',STR_PAD_LEFT);
	
return $array;
}

Initial URL

                                

Initial Description
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.

Initial Title
add leading zeros

Initial Tags
list, array

Initial Language
PHP