我有以下代码:
结果为:
但是如果我把它改成:
结果变成了:
<!DOCTYPE html>
<html >
<head>
<title>Distance</title>
</head>
<body>
<?php
$distance = array(
array(0, 200, 57, 223),
array(200, 0, 150, 5),
array(57, 150, 0, 177),
array(57, 150, 0, 177),
array(223, 5, 177, 0)
);
$cityA = filter_input(INPUT_POST, "cityA");
$cityB = filter_input(INPUT_POST, "cityB");
$result = $distance[$cityA][$cityB];
print "<p> The distance between the 2 cities is $result</p>";
?>
</body>
</html>
对于代码:
print "<p> The distance between the 2 cities is $result</p>";
结果为:
这是结果。
The distance between the 2 cities is 177
但是如果我把它改成:
print "<p> The distance between the 2 cities is $distance[$cityA][$cityB]</p>";
结果变成了:
The distance between the 2 cities is Array[0]
有人能解释一下为什么我直接打印$distance[$cityA][$cityB]时它不被解释为一个二维数组吗?
$cityA
е’Ң$cityB
зҡ„еҖјжҳҜд»Җд№Ҳпјҹ - Martin$distance
数组的键和值都改变,因为我不知道你的脚本如何知道哪个城市有什么坐标。 - David