dongyuan4790 2012-07-26 06:00
浏览 80
已采纳

PHP - 使用while循环创建嵌套数组

I want to make an array like this

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

I always get an error like this:

Parse error: syntax error, unexpected 'while' (T_WHILE), expecting ')' in C:\wamp\www\Tests\index.php on line 11

<!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>
  • 写回答

3条回答 默认 最新

  • douchixu3686 2012-07-26 06:03
    关注
    $a = array();
    
    while($x<=10):
        $a[] = 'firstName'.$x;
        $a[] = 'lastName'.$x;
        $x++;
    endwhile;
    

    I read your question again, if you want this "'firstName1', 'lastName1'" to be actually a string then,

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

    Or based on your question title (nested array) then,

    $x = 1;
    while($x<=10):
       $a[] = array('firstName'.$x, 'lastName'.$x);
       $x++;
    endwhile;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线