项目采用Thinkphp,前端框架用dwz,在页面构造了一个二维数组,如下:
短消息 邮件 微信
提交上去后,打印noticetype这个数组,得到的结果是:
Array
(
[9353] => Array ( ['msg'] => msg ) [9784] => Array ( ['wechat'] => wechat ) [10113] => Array ( ['email'] => email )
)
但是,当使用循环遍历上面这个数组,或者输出:
"$_POST\[noticetype\]\[9784\][\'wechat\"这种格式
却显示空白,是不是post不该这样使用二维数组?
为什么能打印出完整的二维数组却无法获取单个值呢?
用你的给出的现有的数据模拟了一遍,发现是你的提交表格的时候的数组里面的键值加了引号的问题。
给你看看我用你的数据写的:
<?php if($_POST){ echo "<pre>"; print_r($_POST); echo "</pre>"; echo "-----------------------------------------------"."<p>"; echo $_POST['noticetype'][9353]['msg']."<br>"; echo $_POST['noticetype'][9784]['email']."<br>"; echo $_POST['noticetype'][10113]['wechat']."<br>"; echo "---------------------------------------------"."<p>"; foreach ($_POST['noticetype'] as $key => $value) { foreach ($value as $k => $v) { echo $v.'<br>'; } } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>index</title> <link rel="stylesheet" href=""> </head> <body> <table> <thead> <tr> <td> <form action="demo.php" method="post" accept-charset="utf-8"> <input type="checkbox" value="msg" name="noticetype[9353][msg]" />短消息 <input type="checkbox" value="email" name="noticetype[9784][email]" />邮件 <input type="checkbox" value="wechat" name="noticetype[10113][wechat]" />微信 <input type="submit" name="submit" value="提交"> </form> </td> </tr> </thead> </table> </body> </html>
结果展示:
如果你将msg加上引号也就是<input type="checkbox" value="msg" name="noticetype[9353]['msg']" />
这样的话,foreach循环可以取到值但是$_POST['noticetype'][9353]['msg']
这样取不到值。