這裏是我的代碼:PHP陣推繩子,但丟失的零
<?php
$num = array("00000001","1","000","2","13454234","3");
$longnum = array();
$shortnum = array();
foreach($num as $key => $value)
{
if($key % 2 == 0)
{
$longnum[] += $value;
}
else
{
$shortnum[] += $value;
}
}
預期輸出:
Long num: Array ([0] => 00000001 [1] => 000[2] => 13454234)
Short num: Array ([0] => 1 [1] => 2 [2] => 3)
但我的輸出:
Long num: Array ([0] => 1 [1] => 1232 [2] => 13454234)
Short num: Array ([0] => 1 [1] => 2 [2] => 3)
我想獲得長期NUM與預期產出相同。但是,我的輸出不包含數組中的零。我應該怎麼做?
將'+ ='放在這裏,並像這樣'$ longnum [] = $ value'改爲'=';'您迫使PHP通過數學函數將字符串轉換爲數字。 – Fluffeh 2014-09-20 03:54:17
使用[Type Casting](http://php.net/manual/en/language.types.type-juggling.php)值,或者它會被智能猜測'int longnum [] =(string)$價值;' – bansi 2014-09-20 03:56:11
嗨,Fluffeh。你解決了我的問題。 – user3905044 2014-09-20 03:56:27