是的,在 PHP 的关联数组中使用 vars 作为键名是没有问题的,例如:
$a = 1;
$b = 2;
$arrayname['answertext'.$a] = "some sentence string A";
$arrayname['answertext'.$b] = "some sentence string B";
echo $arrayname['answertext'.$a] . "<br/>"; //"some sentence string A"
echo $arrayname['answertext1'] . "<br/>"; //"some sentence string A"
echo $arrayname['answertext'.$b] . "<br/>"; //"some sentence string B"
echo $arrayname['answertext2'] . "<br/>"; //"some sentence string B"
要使用 array( ) 创建关联数组,请使用 => 符号将索引与值分开:
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// a partir de PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
要构造一个空数组,不向 array() 传递任何参数:
$addresses = array( );
有关数组的更多信息:http:
//oreilly.com/catalog/progphp/chapter/ch05.html