1

このような配列を作りたい

$array = array("'firstName1', 'lastName1'", "'firstName2', 'lastName2'", ......);

私はいつもこのようなエラーを受け取ります:

解析エラー: 構文エラー、予期しない 'while' (T_WHILE)、11 行目の C:\wamp\www\Tests\index.php の ')' が必要です

<!doctype html>
<html>
<head>
    <meta charset="utf=8">
</head>
<body>
<?php
if(isset($_POST['reg'])){
    $x=1;
    $a = array(
    while($x<=10):
    "'firstName$x', 'lastName$x'"; //I DONT KNOW WHAT TO DO IN THIS LINE//
    $x++;
    endwhile;
    );
    print_r($a);
}
?>
<form action="" method="post">
<input type="text" name="number" /> <input type="submit" name="submit" value="Submit"/>
</form>
    <form action="" method="post">
    <table>
    <?php
    if(isset($_POST['submit'])){
    for($i=1;$i<=$_POST['number'];$i++){
    echo "<tr>
    <td><input type='text' name='firstName$i' /></td>
    <td><input type='text' name='lastName$i' /></td>
    </tr>";
    }
    $i-=1;
    echo "<input type='hidden' name='hide' value='$i' />";
    }           
    ?>
    </table>
    <input type="submit" value="Register" name="reg"/>
    </form>
</body>
</html>
4

3 に答える 3

7
$a = array();

while($x<=10):
    $a[] = 'firstName'.$x;
    $a[] = 'lastName'.$x;
    $x++;
endwhile;

この「'firstName1'、'lastName1'」を実際に文字列にしたい場合は、もう一度あなたの質問を読みました。

$a = array();
while($x<=10):
    $a[] = 'firstName'.$x.'lastName'.$x;
    $x++;
endwhile; 

または、質問のタイトル (ネストされた配列) に基づいて、

$x = 1;
while($x<=10):
   $a[] = array('firstName'.$x, 'lastName'.$x);
   $x++;
endwhile;
于 2012-07-26T06:03:10.443 に答える
0

これがあなたを助けますように。

$x = 1;
while($x <=10 ):    
    $a[] = '"\'firstName'.$x.'\', \'lastName'.$x.'\'"';    
    $x++;
endwhile;
echo '<pre>';
print_r($a);
echo '</pre>';

出力は次のとおりです。

Array
(
    [0] => "'firstName1', 'lastName1'"
    [1] => "'firstName2', 'lastName2'"
    [2] => "'firstName3', 'lastName3'"
    [3] => "'firstName4', 'lastName4'"
    [4] => "'firstName5', 'lastName5'"
    [5] => "'firstName6', 'lastName6'"
    [6] => "'firstName7', 'lastName7'"
    [7] => "'firstName8', 'lastName8'"
    [8] => "'firstName9', 'lastName9'"
    [9] => "'firstName10', 'lastName10'"
)
于 2012-07-26T06:26:36.193 に答える
-1
if(isset($_POST['reg'])){
$x=1;
$a = array();
while($x<=10){
 $a[] = "firstName$x";
 $a[] = "lastName$x";
 $x++;
}

print_r($a);

}

于 2012-07-26T06:06:01.137 に答える