Heim  >  Artikel  >  Backend-Entwicklung  >  html页面构造的多维数组在服务器端无法获取值

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

WBOY
WBOYOriginal
2016-06-06 20:22:561345Durchsuche

项目采用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>

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

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

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

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

)

但是,当使用循环遍历上面这个数组,或者输出:
"$_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>

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

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

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

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

)

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

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

<code><?php if($_POST){    
    echo "<pre class="brush:php;toolbar:false">";
    print_r($_POST);
    echo "</code>
"; 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']这样取不到值。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn