Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中使用嵌套for循环似乎跳过了所需的值,如何使用array_maps()消除数组中的重复项?_Php_Arrays_R_Loops - Fatal编程技术网

在PHP中使用嵌套for循环似乎跳过了所需的值,如何使用array_maps()消除数组中的重复项?

在PHP中使用嵌套for循环似乎跳过了所需的值,如何使用array_maps()消除数组中的重复项?,php,arrays,r,loops,Php,Arrays,R,Loops,我正在将一些代码从R转换为PHP,这些代码查找数组中两个给定点之间的距离,并将它们与用户给定的cap进行比较 如果距离小于或等于上限,则应将两个相应组中的点放置在称为 $aResults和$bResults。我正在比较使用嵌套for循环的PHP脚本的输出与我在R中编写的更慢但更彻底的算法,以确保我得到了每一点。我还创建了两个计数器变量来跟踪值应该放在匹配数组中的哪个位置,分别称为$aCounter和$bCounter for ($i= 0; $i < count($groupA); $i+

我正在将一些代码从R转换为PHP,这些代码查找数组中两个给定点之间的距离,并将它们与用户给定的cap进行比较

如果距离小于或等于上限,则应将两个相应组中的点放置在称为
$aResults
$bResults
。我正在比较使用嵌套for循环的PHP脚本的输出与我在R中编写的更慢但更彻底的算法,以确保我得到了每一点。我还创建了两个计数器变量来跟踪值应该放在匹配数组中的哪个位置,分别称为
$aCounter
$bCounter

for ($i= 0; $i < count($groupA); $i++)
{
    $bCounter = 0;
    for ($j = 0; $j < count($groupB); $j++)
    {
        if (distance($groupA[$i], $groupB[$j]) <= $cap)
        {
             $aMatches[$aCounter] = $i + 1;
             $bMatches[$bCounter] = $j + 1;
        }
        $bCounter++;
    }
    $aCounter++;
}
for($i=0;$i如果(distance($groupA[$i],$groupB[$j])我不能100%确定将结果放在两个不同的数组中是什么意思。但是我忽略了你想要做的事情

// Two groups of points with each point consisting of [x,y]
$a = [[0,1], [1,1], [2,1], [3,1], [4,1], [10,1]];
$b = [[0,0], [1,0], [2,0], [3,0], [4,0], [10,0]];

$capLength = 1.9;

// Returns all points in a and b that are less than $capLength units of distance
function findPointsLessThanCapDistance($a, $b, $capLength)
{
    $results = [];
    // Loop through first group of points
    foreach ($a as $aIndex => $pointA) {
        // Loop through each point in second group per point in first group
        foreach ($b as $pointB) {
            // Check the distance between points
            if (distance($pointA, $pointB) <= $capLength) {
                // If there's a match, we need to put the results somewhere
                // This is what you'd change to put them into different arrays
                // or format them differently
                if (!array_key_exists($aIndex, $results)) {
                    $results[$aIndex] = [
                        'pointA' => $pointA,
                        'matches' => []
                    ];
                }
                $results[$aIndex]['matches'][] = $pointB;
                // End storing this matching point and move on to next pair
            }
        }
    }

    return $results;
}

// Could be any other calculation
function distance($pointA, $pointB)
{
    return sqrt(($pointA[0] - $pointB[0]) ** 2 + ($pointA[1] - $pointB[1]) ** 2);
}

print_r(findPointsLessThanCapDistance($a, $b, $capLength));

// What results will be afterwards
$results = [
    [
        ['pointA'] => [0,1],
        ['matches'] => [[0,0], [1,0]]
    ],
    [
        ['pointA'] => [1,1],
        ['matches'] => [[0,0], [1,0], [2,0]]
    ],
    etc.
];
//两组点,每个点由[x,y]组成
$a=[[0,1]、[1,1]、[2,1]、[3,1]、[4,1]、[10,1];
$b=[[0,0]、[1,0]、[2,0]、[3,0]、[4,0]、[10,0];
$capLength=1.9;
//返回a和b中小于$capLength距离单位的所有点
函数findPointsLessThanCapDistance($a、$b、$capLength)
{
$results=[];
//循环通过第一组点
foreach($a作为$aIndex=>$pointA){
//在第二组中的每个点上循环第一组中的每个点
foreach($b作为$pointB){
//检查点之间的距离
如果(距离($pointA,$pointB)$pointA,
“匹配项”=>[]
];
}
$results[$aIndex]['matches'][]=$pointB;
//结束存储此匹配点并继续下一对
}
}
}
返回$results;
}
//可能是其他的计算
函数距离($pointA,$pointB)
{
返回sqrt(($pointA[0]-$pointB[0])**2+($pointA[1]-$pointB[1])**2);
}
打印(findPointsLessThanCapDistance($a、$b、$capLength));
//之后会有什么结果
$results=[
[
['pointA']=>[0,1],
['matches']=>[[0,0],[1,0]]
],
[
['pointA']=>[1,1],
['matches']=>[[0,0],[1,0],[2,0]]
],
等
];

注意,这是O(n^2),所以对于大型点集来说不是最快的。如果我们对数据集有更多的边界,就可以进行优化。

奇怪的是,为什么要从R转换为PHP?两者都可以通过命令行调用任何脚本并传递参数。Lol说实话,我希望我能回答这个问题。SofWare必须运行的主计算机没有安装R,而且它们没有安装我不想安装它。希望他们知道R是开源的,可能比PHP占用的空间更小。但请发布一些数据,更完整的代码块,以便我们可以提供帮助。目前代码不可编译,我们不知道源数据或所需结果。甚至R代码也可以提供帮助。