Link to home
Start Free TrialLog in
Avatar of trrsrr
trrsrrFlag for United States of America

asked on

How to avoid zero(s) in front of number(s) disappears in array

Hi experts...
I included my simplified codes below.

Straight to the example so you can get a better view for this case :

Let say I have $data = '1-5';
and my code turns it correctly into:
1
2
3
4
5

But if I have $data = '01-05';
then my code turns it into (not correct, not what I want):
01
2
3
4
5

... where the correct result (that I want) should be :
01
02
03
04
05

And the results are always not correct if I have :
$data = '01-05';
or...
$data = '0021-0025';
or maybe...
$data = '000101-000108';

So, seems like zero(s) in front of the numbers are disappear.. only the top/first array that is displayed correctly.

Any advance help for this case please ...
<?php

$data = '1-5';
//$data = '01-05';
//$data = '0021-0025';
//$data = '000101-000108';

	list($start_at, $end_at) = split('-', $data); 

	$arr_data = ''; 
	for ($i = $start_at; $i <= $end_at; $i++) 
		{
			$arr_data .= $i.'<br>';
		}

echo $arr_data;

?>

Open in new window

Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Try the code below

http://www.php.net/intval
The code didn't attach properly.
<?php

$data = '1-5';
//$data = '01-05';
//$data = '0021-0025';
//$data = '000101-000108';

     list($start_at, $end_at) = split('-', $data); 

     $arr_data = ''; 
     for ($i = intval($start_at); $i <= intval($end_at); $i++) 
          {
               $arr_data .= $i.'<br>';
          }

echo $arr_data;

?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trrsrr

ASKER

@bportlock:
I believe you got my point wrong, I dont want to remove all available zero(s) ... I want those available zero(s) to appears.

So, for example I have $data = '000101-000105';
Then the result should be:
000101
000102
000103
000104
000105

while your codes will give me this result:
101
102
103
104
105
Avatar of trrsrr

ASKER

Ha ha ... u're cool man.

Thanks for the revised codes. It's awesome...  ^_^
Hey - glad it's OK for you. I'm off for a good night's kip.

Good luck with your code & project.