嵌套循环使用foreach的相同数据仅运行第一个循环的第一个数据

时间:2017-01-10 04:26:03

标签: php cakephp nested-loops

我有一个像这样的嵌套循环

foreach ($subdistricts as $subdistrict) {
    $originId = $subdistrict->id;
    $exist[] = $originId; 
    foreach($subdistricts as $subdistrict) {
        if(($originId != $subdistrict->id) || (!in_array($subdistrict->id,$exist))) {
            $destinationId = $subdistrict->id;
            echo 'from: '.$originId.' to: ' .$destinationId;
        }
    }
}

让我们说我的数据ID是这样的= 244,255,266

我希望结果如下:

  来自' 244'到' 255'

     来自' 244'到'''

     来自' 255'到'''

相反,我只得到了:

  来自' 244'到' 255'

     来自' 244'到'''

$ subdistricts:

Cake\ORM\Query Object
(
    [(help)] => This is a Query object, to get the results execute or iterate it.
    [sql] => SELECT Subdistricts.id AS `Subdistricts__id`, Subdistricts.name AS `Subdistricts__name`, Subdistricts.city_id AS `Subdistricts__city_id` FROM subdistricts Subdistricts WHERE Subdistricts.city_id = :c0
    [params] => Array
        (
            [:c0] => Array
                (
                    [value] => 17
                    [type] => integer
                    [placeholder] => c0
                )

        )

    [defaultTypes] => Array
        (
            [Subdistricts__id] => integer
            [Subdistricts.id] => integer
            [id] => integer
            [Subdistricts__name] => string
            [Subdistricts.name] => string
            [name] => string
            [Subdistricts__city_id] => integer
            [Subdistricts.city_id] => integer
            [city_id] => integer
        )

    [decorators] => 1
    [executed] => 1
    [hydrate] => 1
    [buffered] => 1
    [formatters] => 0
    [mapReducers] => 0
    [contain] => Array
        (
        )

    [matching] => Array
        (
        )

    [extraOptions] => Array
        (
        )

    [repository] => App\Model\Table\SubdistrictsTable Object
        (
            [registryAlias] => Subdistricts
            [table] => subdistricts
            [alias] => Subdistricts
            [entityClass] => App\Model\Entity\Subdistrict
            [associations] => Array
                (
                    [0] => cities
                )

            [behaviors] => Array
                (
                )

            [defaultConnection] => default
            [connectionName] => default
        )

)

$ subdistrict:

App\Model\Entity\Subdistrict Object
(
    [id] => 258
    [name] => Abiansemal
    [city_id] => 17
    [[new]] => 
    [[accessible]] => Array
        (
            [*] => 1
        )

    [[dirty]] => Array
        (
        )

    [[original]] => Array
        (
        )

    [[virtual]] => Array
        (
        )

    [[errors]] => Array
        (
        )

    [[invalid]] => Array
        (
        )

    [[repository]] => Subdistricts
)

3 个答案:

答案 0 :(得分:0)

假设您的数据是[244,255,266],您可以使用这段代码。我从here复制了它。

function combination_number($k,$n){
    $n = intval($n);
    $k = intval($k);
    if ($k > $n){
        return 0;
    } elseif ($n == $k) {
        return 1;
    } else {
        if ($k >= $n - $k){
            $l = $k+1;
            for ($i = $l+1 ; $i <= $n ; $i++)
                $l *= $i;
            $m = 1;
            for ($i = 2 ; $i <= $n-$k ; $i++)
                $m *= $i;
        } else {
            $l = ($n-$k) + 1;
            for ($i = $l+1 ; $i <= $n ; $i++)
                $l *= $i;
            $m = 1;
            for ($i = 2 ; $i <= $k ; $i++)
                $m *= $i;            
        }
    }
    return $l/$m;
}

function array_combination($le, $set){

    $lk = combination_number($le, count($set));
    $ret = array_fill(0, $lk, array_fill(0, $le, '') );

    $temp = array();
    for ($i = 0 ; $i < $le ; $i++)
        $temp[$i] = $i;

    $ret[0] = $temp;

    for ($i = 1 ; $i < $lk ; $i++){
        if ($temp[$le-1] != count($set)-1){
            $temp[$le-1]++;
        } else {
            $od = -1;
            for ($j = $le-2 ; $j >= 0 ; $j--)
                if ($temp[$j]+1 != $temp[$j+1]){
                    $od = $j;
                    break;
                }
            if ($od == -1)
                break;
            $temp[$od]++;
            for ($j = $od+1 ; $j < $le ; $j++)    
                $temp[$j] = $temp[$od]+$j-$od;
        }
        $ret[$i] = $temp;
    }
    for ($i = 0 ; $i < $lk ; $i++)
        for ($j = 0 ; $j < $le ; $j++)
            $ret[$i][$j] = $set[$ret[$i][$j]];

    $res = json_decode(json_encode($ret), false);
    return $res;
}

在您的情况下,用法如下

$data = [244,255,266];

$result = array_combination(2, $data);
for($i=0;$i<count($result);$i++)
{
    print "from ".$result[$i][0]." to ".$result[$i][1]."\n";
}

输出

from 244 to 255
from 244 to 266
from 255 to 266

答案 1 :(得分:0)

foreach ($subdistricts as $subdistrict){
    $originId = $subdistrict->id;
    $exist[] = $originId; 
    foreach($subdistricts as $subdistrict_1){
        if(($originId != $subdistrict_1->id) || in_array($subdistrict_1->id,$exist))){
           $destinationId = $subdistrict_1->id;
           echo 'from: '.$originId.' to: ' .$destinationId;
        }
    }
}

您可以尝试使用此代码,因为您的代码$ subdistrict变量在第二个foreach中发生冲突。

答案 2 :(得分:0)

  1. 由于您正在使用foreach,因此它会创建/使用变量$subdistrict两次并被覆盖,因此您需要更改其中任何一个(我将嵌套的更改为$subdistrict_1 )。
  2. if语句中,您验证它不是相同的 OR 它不在数组中,而您需要它不相同< b> AND 不在数组中。

    foreach ($subdistricts as $subdistrict) {
        $originId = $subdistrict->id;
        $exist[] = $originId; 
    
        foreach($subdistricts as $subdistrict_1){
            if( ($originId != $subdistrict_1->id) && !in_array($subdistrict_1->id, $exist) ) {
                $destinationId = $subdistrict_1->id;
                echo 'from: '.$originId.' to: ' .$destinationId;
            }
        }
    }