Reputation: 1442
I have some bar code numbers in an array. PHP seems to be rounding the barcodes which start with leading zeros. How do I stop this happening and keep the numbers as they were? Code I am using is below:
$array = array(5032227448124,5060028999989,5010121096504,5060028999996,5016254104864,5016402052788,8422248036986,0000003798720,0000003735503,0000003798713);
echo '<pre>';
print_r($array);
echo '</pre>';
This echos the following, as you can see the last four bar codes which feature leading zeros have been changed and had their leading zeros removed. These numbers are always 13 digits long and are padded with zeros.
Array ( [0] => 5032227448124 [1] => 5060028999989 [2] => 5010121096504 [3] => 5060028999996 [4] => 5016254104864 [5] => 5016402052788 [6] => 8422248036986 [7] => 31 [8] => 1030979 [9] => 31 [10] => 1031004 )
Upvotes: 0
Views: 213
Reputation: 1442
In the end I just needed to put double quotes around each barcode number e.g.
$array = array("5032227448124","5060028999989","5010121096504","5060028999996","5016254104864","5016402052788","8422248036986","0000003798720","0000003735503","0000003798713");
Upvotes: 0
Reputation: 7795
Here's an easy way to convert your values to padded strings:
$array = array_map(function ($e){return str_pad($e, 13, "0", STR_PAD_LEFT);}, $array);
Upvotes: 0
Reputation: 32232
The obvious, easy, and also likely wrong answer is to make them strings.
The better answer is to use printf()
/sprintf()
to pad with zeroes:
printf('%013d', 12345); // output: 0000000012345
MySQL also has a handy LPAD()
function:
SELECT LPAD(12345, 13, 0) // output 0000000012345
Upvotes: 0
Reputation: 60413
You need to quote them as strings if they arent a number (integer, float, exponent).
Upvotes: 2