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 )

我应该怎么办?你能帮助我吗?谢谢。

4

4 回答 4

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  
    ...  
)
于 2013-05-01T10:09:21.207 回答
1

你在哪里使用过array_merge。很难说出你到底卡在哪里了。但是,如果您尝试创建 $length 数组,只需使用array_push()

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

您将在 $final 数组中获得所需的数组

于 2013-05-01T11:05:52.007 回答
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);

希望能帮助到你!

于 2013-05-01T10:30:24.797 回答
0

使用数组函数:array_merge($array1, $array2);

于 2013-05-01T10:05:13.267 回答