我想合并所有数组。这是我的代码:
...
$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 :(得分: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 :(得分: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数组中获得所需的数组
答案 2 :(得分:0)
使用数组函数: array_merge($ array1,$ array2);
答案 3 :(得分: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);
希望它有所帮助!