Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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循环中的每个值?_Php - Fatal编程技术网

Php 如何比较for循环中的每个值?

Php 如何比较for循环中的每个值?,php,Php,我有一个由5个项目组成的多维数组,我希望我的循环能够像这样进行比较: 1 -> 2, 1 -> 3, 1 -> 4, 1 -> 5, 2->1, 2->3, 2->4, 2->5......// so on and 5 -> 4 in the end. 问题是,在我的数组$i值与1匹配,$j值与3匹配之后,未设置的值就完成了,$i值变为2(这是正确的,$j值变为4,而不是3。有人能告诉我为什么和我做错了什么吗 我的循环是: for

我有一个由5个项目组成的多维数组,我希望我的循环能够像这样进行比较:

1 -> 2, 1 -> 3, 1 -> 4, 1 -> 5, 2->1, 2->3, 2->4, 2->5......// so on and 5 -> 4 in the end.
问题是,在我的数组$i值与1匹配,$j值与3匹配之后,未设置的值就完成了,$i值变为2(这是正确的,$j值变为4,而不是3。有人能告诉我为什么和我做错了什么吗

我的循环是:

    for ($i = 0; $i <= count($myArray); $i++) {
        for ($j = $i+1; $j <= count($myArray); $j++) {
            if (
                // condition 1
                && // condition 2
            ) {
                unset($myArray[$i]);
                $i++;
            }
        }
    }

对于($i=0;$i请尝试以下内容

  for ($i = 0; $i <= count($myArray); $i++) {
    for ($j = 0; $j <= count($myArray); $j++) {
        if ($j!=$i)
        {
             if (
            // condition 1
            && // condition 2
        ) {
            unset($myArray[$i]);
            $i++;
        }
        }

    }
}
用于($i=0;$i
$temp=$myArray;

对于($i=0;$i我认为这是问题所在,当您取消设置数组中的元素时,您将增加循环的计数器
$i
。这样,未配置的数组元素将被删除,这个空数组位置将被保留,它将不会被重新排序,您将不得不手动或使用
数组\u值执行此操作​​方法

在数组的最后一次巡更中,它将中断,因为您将数组元素的数量比较为相等。您必须使用
索引

代码如下所示:

for ($i = 0; $i < count($myArray); $i++) {
        for ($j = $i+1; $j < count($myArray); $j++) {
            if (
                // condition 1
                && // condition 2
            ) {
                unset($myArray[$i]);
                // $i++;
            }
        }
    }
for($i=0;$i
老实说,我不知道为什么还没有人建议使用嵌套foreach,因为你们都注意到数组大小有问题

foreach ($numbers as $number_horizontal_parsing) {
    foreach ($numbers as $number_vertical_parsing) {
        if ($number_horizontal_parsing != $number_vertical_parsing) {
            //do your stuff in your case it seems you want to compare both variables
        }
    }
}

永远不要删除循环中的数组元素。您正在减小数组的总大小,这会导致意外行为。因为您正在杀死一个元素,其他元素的元素索引将保留。我认为您是对的。我注意到,当有2-3个数组元素时,在我向数据库添加更多元素之前,它工作正常。您在尝试什么要做什么?要么我需要更多的咖啡,要么你需要给我们提供更多的细节。你能在
$myArray
中添加准确的内容吗?@DevDonkey好吧,这是一个简单的多维数组,没有别的。要么你不删除数组中的元素,要么你将初始长度设置为一个变量。在编写“$i这正是我所拥有的代码。我将数组放入变量中,并最终返回它。:(请使用输入数组和预期输出数组更新您的问题,以避免进一步的歧义。
foreach ($numbers as $number_horizontal_parsing) {
    foreach ($numbers as $number_vertical_parsing) {
        if ($number_horizontal_parsing != $number_vertical_parsing) {
            //do your stuff in your case it seems you want to compare both variables
        }
    }
}