2013-05-01 88 views
1

我想合併所有陣列。這是我的代碼:將所有陣列合併到一個陣列

... 
$j = array(); 
    foreach($result as $document) 
    { 
     $d1 = $document->w; 
     $d2 = $w; 
     $length = sqrt(pow($d1-$d2, 2)); 
     $i++; 
     echo "<tr><td>$i</td><td>$d1 - $d2</td><td>=</td><td>$length</td></tr>";  
     settype($length, "array"); 
     print_r($length); 
    }... 

但是這是我的結果(array_merge將無法正常工作):

Array ([0] => 38.9944) Array ([0] => 6.1042) Array ([0] => 18.7779) Array ([0] => 11.8349) Array ([0] => 3.2698) Array ([0] => 9.2999) Array ([0] => 7.7512) Array ([0] => 5.4868) Array ([0] => 31.1248) Array ([0] => 6.1896) Array ([0] => 9.2577) Array ([0] => 17.8437) Array ([0] => 10.7357) Array ([0] => 0.8981) Array ([0] => 8.7666) Array ([0] => 2.7877) Array ([0] => 27.7104) Array ([0] => 10.3047) Array ([0] => 19.0708) Array ([0] => 14.5354) Array ([0] => 17.03) Array ([0] => 3.4082) Array ([0] => 22.3757) Array ([0] => 9.7722) Array ([0] => 11.778) Array ([0] => 3.7674) Array ([0] => 15.5889) Array ([0] => 0.6329) Array ([0] => 24.1031) Array ([0] => 10.2158) Array ([0] => 1.5809) Array ([0] => 13.4723) Array ([0] => 4.8482) Array ([0] => 0.989) Array ([0] => 15.974) Array ([0] => 13.8557) Array ([0] => 2.7523) Array ([0] => 26.0451) Array ([0] => 9.049) Array ([0] => 2.6922) Array ([0] => 18.664) Array ([0] => 2.6961) Array ([0] => 0.8077) Array ([0] => 21.3762) Array ([0] => 29.8023) Array ([0] => 11.1632) Array ([0] => 19.1626) Array ([0] => 9.9158) Array ([0] => 5.108) Array ([0] => 12.8582) Array ([0] => 4.3698) Array ([0] => 24.3916) Array ([0] => 18.6804) Array ([0] => 1.9838) Array ([0] => 40.2745) Array ([0] => 10.9428) Array ([0] => 14.5778) Array ([0] => 8.443) Array ([0] => 2.2244) Array ([0] => 3.9861) Array ([0] => 4.2342) Array ([0] => 8.043) Array ([0] => 18.3285) Array ([0] => 13.3765) Array ([0] => 2.0827) Array ([0] => 7.2584) Array ([0] => 3.7701) Array ([0] => 23.3415) Array ([0] => 26.3657) Array ([0] => 25.3173) 

我應該怎麼辦?你可以幫我嗎?謝謝。

+0

array_merge的什麼輸出?以及你如何使用它? – Rishabh 2013-05-01 10:08:48

回答

0

你可以嘗試array_push功能,

$new_arr = array(); 
foreach($result as $document) 
{ 
$d1 = $document->w; 
$d2 = $w; 
$length = sqrt(pow($d1-$d2, 2)); 
$i++; 
echo "<tr><td>$i</td><td>$d1 - $d2</td><td>=</td><td>$length</td></tr>"; 
array_push($new_arr,$length); 
} 
print_r($new_arr); 

希望它能幫助!

+0

謝謝你,它的作品! – bob 2013-05-01 11:39:11

0

使用數組的函數:array_merge($ array1,$ array2);

+0

不知道你是如何錯過它的:'但這是我的結果(array_merge不會工作):'。 – h2ooooooo 2013-05-01 10:08:00

1

如果我明白你想要做什麼,那麼你在錯誤的軌道上(沒有理由將其轉換爲數組並將它們合併 - 只需將值推送到循環外部的數組)。

$j = array(); 
foreach($result as $document) 
{ 
    $d1 = $document->w; 
    $d2 = $w; 
    $length = sqrt(pow($d1-$d2, 2)); 
    $i++; 
    echo "<tr><td>$i</td><td>$d1 - $d2</td><td>=</td><td>$length</td></tr>"; 
    $j[] = $length; //No need to cast anything 
} 
print_r($j); 

結果:

Array ( 
    [0] => 38.9944 
    [1] => 6.1042 
    [2] => 18.7779 
    [3] => 11.8349 
    [4] => 3.2698 
    ... 
) 
1

你在哪裏使用過array_merge。有點難以分辨你究竟在哪裏卡住了。但是,如果你想創建$長的陣列只使用array_push()

$final = array() 
foreach(){ 
    $d1 = $document->w; 
    $d2 = $w; 
    $length = sqrt(pow($d1-$d2, 2)); 
    array_push($final,$length); 
} 

你會得到你想要的數組$最終陣列

+0

thanks..it works! – bob 2013-05-01 11:40:34