html页面构造的多维数组在服务器端无法获取值

WBOY
Release: 2016-06-06 20:22:56
Original
1379 people have browsed it

项目采用Thinkphp,前端框架用dwz,在页面构造了一个二维数组,如下:

<code><input type="checkbox" value="msg" name="noticetype[{$vo['staffId']}]['msg']">短消息 
<input type="checkbox" value="email" name="noticetype[{$vo['staffId']}]['email']">邮件 
<input type="checkbox" value="wechat" name="noticetype[{$vo['staffId']}]['wechat']">微信 </code>
Copy after login
Copy after login

提交上去后,打印noticetype这个数组,得到的结果是:
Array
(

<code>[9353] => Array
    (
        ['msg'] => msg
    )

[9784] => Array
    (
        ['wechat'] => wechat
    )

[10113] => Array
    (
        ['email'] => email
    )
</code>
Copy after login
Copy after login

)

但是,当使用循环遍历上面这个数组,或者输出:
"$_POST\[noticetype\]\[9784\][\'wechat\"这种格式
却显示空白,是不是post不该这样使用二维数组?
为什么能打印出完整的二维数组却无法获取单个值呢?

回复内容:

项目采用Thinkphp,前端框架用dwz,在页面构造了一个二维数组,如下:

<code><input type="checkbox" value="msg" name="noticetype[{$vo['staffId']}]['msg']">短消息 
<input type="checkbox" value="email" name="noticetype[{$vo['staffId']}]['email']">邮件 
<input type="checkbox" value="wechat" name="noticetype[{$vo['staffId']}]['wechat']">微信 </code>
Copy after login
Copy after login

提交上去后,打印noticetype这个数组,得到的结果是:
Array
(

<code>[9353] => Array
    (
        ['msg'] => msg
    )

[9784] => Array
    (
        ['wechat'] => wechat
    )

[10113] => Array
    (
        ['email'] => email
    )
</code>
Copy after login
Copy after login

)

但是,当使用循环遍历上面这个数组,或者输出:
"$_POST\[noticetype\]\[9784\][\'wechat\"这种格式
却显示空白,是不是post不该这样使用二维数组?
为什么能打印出完整的二维数组却无法获取单个值呢?

用你的给出的现有的数据模拟了一遍,发现是你的提交表格的时候的数组里面的键值加了引号的问题。
给你看看我用你的数据写的:

<code><?php if($_POST){    
    echo "<pre class="brush:php;toolbar:false">";
    print_r($_POST);
    echo "</code>
Copy after login
"; echo "-----------------------------------------------"."

"; echo $_POST['noticetype'][9353]['msg']."
"; echo $_POST['noticetype'][9784]['email']."
"; echo $_POST['noticetype'][10113]['wechat']."
"; echo "---------------------------------------------"."

"; foreach ($_POST['noticetype'] as $key => $value) { foreach ($value as $k => $v) { echo $v.'
'; } } } ?>

index
短消息 邮件 微信

结果展示:

html页面构造的多维数组在服务器端无法获取值

如果你将msg加上引号也就是<input type="checkbox" value="msg" name="noticetype[9353]['msg']">这样的话,foreach循环可以取到值但是$_POST['noticetype'][9353]['msg']这样取不到值。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template