首页 > 解决方案 > 在html表单数组中使用空键

问题描述

在 html 表单中,可以将数据作为数组发送。

<!-- This is an associative array -->
<input name="table[key1]">
<input name="table[key2]">

<!-- This is an index-based array -->
<input name="table[]">
<input name="table[]">

是否可以使用空字符串作为键将值存储到关联数组中?
这样php就可以访问数据了$_POST["table"][null]

标签: phphtmlformspost

解决方案


null不是空字符串,应该是 a''或 a ""。但是,PHP 会将其转换为空字符串并将其用作键(在示例 1 下查看https://www.php.net/manual/en/language.types.array.php

所以这些是等价的:

$a = array('' => 'hello');
$b = array(null => 'hello');

从您的示例中,数据将在$_POST["table"][""]


推荐阅读