我在PHP中有一個$線像下面你怎麼把一個行到一個數組在PHP
33 0 0 51037280 396822 15 16 0 0 0 0 20907 143197 57377 27 12 51 10 7.33 45.8 17:07:31
我嘗試創建和數組,並把由空間分隔的每個值這個數組是這樣的:
$parts=explode(" ", $line)
應該有20個索引,這$行上有21個coluns。但是當我這樣做:
$size=sizeof($parts)
echo $ size returns 58,這怎麼可能。有沒有更好的方法插入到$行中的項目到數組?看起來像有不規則的值之間的空格。當我這樣做:
的print_r($部分)
我得到如下:
[0] => [1] => 33 [2] => [3] => [4] => 0 [5] => [6] => [7] => 0 [8] => [9] => [10] => 51037280 [11] => [12] => [13] => [14] => [15] => 396822 [16] => [17] => [18] => [19] => 15 [20] => [21] => [22] => [23] => 16 [24] => [25] => [26] => [27] => [28] => 0 [29] => [30] => [31] => [32] => [33] => 0 [34] => [35] => [36] => [37] => [38] => 0 [39] => [40] => [41] => [42] => [43] => [44] => 0 [45] => 20907 [46] => 143197 [47] => 57377 [48] => 27 [49] => 12 [50] => 51 [51] => 10 [52] => [53] => 7.33 [54] => [55] => 45.8 [56] => 17:07:31)
我怎麼會忽略的值之間的空格數和每個值存儲爲一個數組值PHP的?
var_dump($ parts)'show?如果真的有21列,你會得到21的數量,所以看看你實際得到什麼。 –
您可能想檢查一下您的代碼是否真實地再現了您的問題:'echo(sizeof(explode(「」,「,35 0 0 51020880 462460 6 23 0 0 0 0 21770 142334 67212 31 12 46 11 8.13 50.8 16:07:29 58" ))); #22' – meagar
您發佈的代碼,工作完好,我在http://writecodeonline.com/php/中測試,並且其工作完美....返回22 – KyleK