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 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端