【问题标题】:Nested looping same data with foreach only run first data for the first loop使用 foreach 嵌套循环相同数据仅运行第一个循环的第一个数据
【发布时间】:2017-01-10 04:26:03
【问题描述】:

我有一个这样的嵌套循环

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”到“266”

从“255”到“266”

相反,我只得到:

从“244”到“255”

从“244”到“266”

$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
        )

)

$ 分区:

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
)

【问题讨论】:

  • 您的$subdistrict 数组包含什么?请打印并显示在上面。
  • foreach ($subdistricts as $subdistrict) 再次进入循环?
  • 你能给我整个 $subdistricts 数组吗??
  • 这是错误的$subdistricts 数组发布
  • @shakylmansuri 我已经编辑了我的问题

标签: php cakephp nested-loops


【解决方案1】:

假设您的数据是[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

【讨论】:

    【解决方案2】:
    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 中存在冲突。

    【讨论】:

    • 你能给我整个 $subdistricts 数组吗??
    【解决方案3】:
    1. 由于您使用的是foreach,它会创建/使用变量$subdistrict 两次并被覆盖,因此您需要更改其中一个(我将嵌套的一个更改为$subdistrict_1)。
    2. if 语句中,您正在验证它不一样 OR 它不在数组中,而您需要它不一样 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;
              }
          }
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-30
      • 2021-09-11
      • 2012-09-03
      • 1970-01-01
      • 2013-08-04
      • 2016-09-28
      • 2021-05-26
      • 1970-01-01
      相关资源
      最近更新 更多