if ($source=="test") {
header("Location: $testurl['google']");
}如果url是example.com/redirect.php?source= google.com,则应该重定向到测试,但它不起作用。如果我将数组键更改为1,并将位置更改为$testurl1,它确实可以工作。为什么它适用于索引数组,而不适用于关联数组?
发布于 2020-01-07 12:37:16
假设您在某处有一个元素为google的数组$testurl
$testurl = [
'google' => 'https://google.pl'
];然后你可以像这样使用它:
if ($source=="test") {
header("Location: $testurl[google]");
}或
if ($source=="test") {
header("Location: {$testurl['google']}");
}或者在我看来最好的选择,就像这样:
if ($source=="test") {
header('Location: ' . $testurl['google']);
}它的定义很简单:https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
有一句话:
// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";https://stackoverflow.com/questions/59622158
复制相似问题