为什么这个PHP数组直接访问不起作用?

时间:2014-07-19 15:42:03

标签: php arrays type-conversion

print_r($ testarray)给出:

Array
(
    [1] => Array
        (
            [id] => 1
            [account] => testuser
            [sum] => 152
            [sumrate] => 0.08
            [avgrate] => 10.133333333333
            [speed] => 14167.426844444
        )

)

echo "Speed for User ID 1: $testarray[1][speed]";

只是给出:

PHP注意:数组转换为字符串

我做错了什么?

4 个答案:

答案 0 :(得分:0)

您不能简单地打印一个Array对象。

$ testarray [1]是一个数组。

使用print_r()

或者您可以单独从$ testarray [1]中获取值:

$speed = $testarray[1]["speed"];

答案 1 :(得分:0)

echo"用户ID 1的速度:"。$ testarray [1] [' speed']; 试试这个

答案 2 :(得分:0)

echo $testarray[1]['speed']

echo $testarray[1][speed]

答案 3 :(得分:0)

因为你没有封装或者数组,或者你想要回应什么,所以PHP可以理解你想要回应的内容,它回显$testarray[1]这是一个数组,然后是字面[speed]

echo "Speed for User ID 1: " . $testarray[1]['speed'];

echo "Speed for User ID 1: {$testarray[1]['speed']}";
相关问题