开发者

What's wrong in this loop?

I checked and i don't found errors:

$_SESSION['variable'] = 2 
$prepa开发者_开发百科rado[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>";
}

I recibe this:

MiguelBeltran
CarlosBeltran
CarlosSanz
CarlosSanz

But I should recibe this:

MiguelBeltran
CarlosBeltran
MiguelSanz
CarlosSanz

Thanks!!


I guess you intended

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


The code you posted is not consistent with the output you presented:

  1. There's no way you get a Miguel in your output as you do:

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

    so the only variable you assign Miguel to, is overwritten the next line.

  2. There is no variable $preparado1.

You should post the code you actually used to generate the output you got.


Given:

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

Use:

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


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

You assigned $preparado[1] twice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜