我有一个下面格式的数据数组,我发现很难用php访问元素,特别是“data”数组。
Array (
[jsonrpc] => 2.0
[result] => Array (
[random] => Array (
[data] => Array (
[0] => 15
[1] => 36
[2] => 11
[3] => 2
[4] => 31
[5] => 16
[6] => 19
[7] => 50
[8] => 24
[9] => 33
[10] => 3
[11] => 46
[12] => 37
[13] => 7
[14] => 29
[15] => 13
[16] => 52
[17] => 14
[18] => 49
[19] => 42
[20] => 40
[21] => 25
[22] => 39
[23] => 21
[24] => 28
[25] => 27
[26] => 34
[27] => 8
[28] => 10
[29] => 41
[30] => 45
[31] => 47
[32] => 26
[33] => 20
[34] => 12
[35] => 9
[36] => 6
[37] => 32
[38] => 18
[39] => 17
[40] => 38
[41] => 23
[42] => 5
[43] => 51
[44] => 35
[45] => 43
[46] => 44
[47] => 30
[48] => 48
[49] => 4
[50] => 1
[51] => 22
)
[completionTime] => 2016-01-22 11:11:26Z
)
[bitsUsed] => 296
[bitsLeft] => 933024
[requestsLeft] => 197725
[advisoryDelay] => 70
)
[id] => 16083686 )
我试过了
echo $myArray['data'];
但我收到错误未定义索引:数据
我也尝试过foreach函数
foreach ($myArray as $row){
echo $row['result']['random']['data'];
}
但我收到错误非法字符串偏移'结果'
我已经有了这个时间,并且非常乐意获得访问此数组中元素的帮助。
答案 0 :(得分:1)
您必须指定访问项目的完整路径
echo $row['result']['random']['data'][0];
$ row ['result'] ['random'] ['data']是一个数组,函数echo不能显示数组。要显示数组,请使用print_r()或var_dump()。
var_dump($row['result']['random']['data']);
print_r($row['result']['random']['data']);
如果您想访问可以使用的所有数据项:
foreach($row['result']['random']['data'] as $k => $v) {
echo $k . " = " . $v . "<br />";
}
答案 1 :(得分:0)
你的元素存储在这样的东西中,尝试使用变量作为索引并循环它,以获得所有值。
echo $row['result']['random']['data'][0];
echo $row['result']['random']['data'][1];
echo $row['result']['random']['data'][2];