我想知道,如果我们把一个空字符串作为数组的键,是否可以这样做。
$test = array (
'' => 'Select',
1 => 'Internal',
2 => 'External'
);
有任何建议将不胜感激。
数组键应该是整数或字符串。
使用空字符串作为数组键并不是理想的做法。
另外,Null将被转换为空字符串,即key null实际上将存储在 "" 下面。
但这种方法会使您的数组具有歧义和不确定性。
考虑以下示例:
$test = array (
'' => 'Select',
1 => 'Internal',
2 => 'External',
'' => 'select'
);
var_dump(array_key_exists('', $test)); // output 'bool(true)', not so bad - but only at first glance
var_dump($test[""]); // output "select"
var_dump((object) $test);
// output: object(stdClass)#1 (3) { E_NOTICE : type 8 -- Illegal member variable name -- at line 12 [""]=> string(6) "select" [1]=> string(8) "Internal" [2]=> string(8) "External" }
<select>
菜单时使用它。 - Clay