Here is my code:
<?php
$num = array("00000001","1","00001232","2","13454234","3");
$longnum = array();
$shortnum = array();
foreach($num as $key => $value)
{
if($key % 2 == 0)
{
$longnum[] += $value;
}
else
{
$shortnum[] += $value;
}
}
Expected output:
Long num: Array ( [0] => 00000001 [1] => 00001232 [2] => 13454234 )
Short num: Array ( [0] => 1 [1] => 2 [2] => 3 )
But my output:
Long num: Array ( [0] => 1 [1] => 1232 [2] => 13454234 )
Short num: Array ( [0] => 1 [1] => 2 [2] => 3 )
I want to get the long num same as the expected output. However, my output doesn't included the zero inside an array. How should I do?
+=in here and change to=like this$longnum[] = $value;You are forcing PHP to typecast the string to a number through the math function.$longnum[] = (string)$value;