这个循环有什么问题?

时间:2011-04-12 18:30:55

标签: php

我检查过,我没有发现错误:

$_SESSION['variable'] = 2 
$preparado[1] = "Miguel";
$preparado[2] = "Carlos";
$segundo[1]= "Beltran";
$segundo[2] = "Sanz";

for($i = 1; $i <=$_SESSION['variable']; $i++){
  $listo[$i] = $preparado1[$i] . $segundo[1];
}


for($i = ($_SESSION['variable'] + 1) ; $i <= ($_SESSION['variable'] * 2); $i++){
  for($n = 1; $n <=$_SESSION['variable']; $n++){
    $listo[$i] = $preparado1[$n] . $segundo[2];
  }
}

for($i = 1;$i <=$_SESSION['variable'] * $_SESSION['extension']; $i++ ){
  echo   $final[$i] . "</br>";
}

我记得这个:

MiguelBeltran
CarlosBeltran
CarlosSanz
CarlosSanz

但我应该重申:

MiguelBeltran
CarlosBeltran
MiguelSanz
CarlosSanz

谢谢!

4 个答案:

答案 0 :(得分:3)

我猜你有意

$preparado[0] = "Miguel"; // rather than [1]
$preparado[1] = "Carlos";

答案 1 :(得分:1)

您发布的代码与您提供的输出不一致:

  1. 您的输出中无法获得Miguel

    $preparado[1] = "Miguel";
    $preparado[1] = "Carlos";
    

    因此,您指定Miguel的唯一变量将覆盖下一行。

  2. 没有变量$preparado1

  3. 您应该发布实际用于生成输出的代码。

答案 2 :(得分:1)

假设:

$_SESSION['variable'] = 2 
$preparado[1] = "Miguel";
$preparado[2] = "Carlos";
$segundo[1]= "Beltran";
$segundo[2] = "Sanz";

使用:

for ($i = 1; $i <= $_SESSION['variable']; ++$i) {
    for ($j = 1; $j <= $_SESSION['variable']; ++$j) {
        $listo[] = $preparado[$j].$segundo[$i];
    }
}

答案 3 :(得分:0)

$preparado[1] = "Miguel";
$preparado[1] = "Carlos";
$segundo[1]= "Beltran";
$segundo[2] = "Sanz";

您已分配$preparado[1]两次。