3

我有以下代码:

<!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] 时不将其解释为二维数组?

4

1 回答 1

4

试着用花括号把它包起来。

print "<p> The distance between the 2 cities is {$distance[$cityA][$cityB]}</p>";
于 2012-08-14T14:35:15.410 回答