嗨,我正在努力解决这个问题。
我有一个javascript数组,我通过$ POST使用:
发送给PHPjsonScrutineeringArray = JSON.stringify(scrutineeringArray);
PHP中的我做了:
$jsonScrutineeringArray = $_REQUEST["jsonScrutineeringArray"];
$tempData = html_entity_decode($jsonScrutineeringArray);
$jsonScrutineeringArray2 = json_decode($tempData);
echo json_encode($jsonScrutineeringArray2);
for ($ii = 0; $ii < count($jsonScrutineeringArray2); ++$ii) {
echo 'driverID ='.$jsonScrutineeringArray2['driverID'][$ii];
}
第一个回声的结果是:
[{"ID":0,"driverID":"1","event":"Silverstone 21\/2\/2015","race":"Race2","FRTagID":"11","FRDepth":"11","FRNotes":"11","FRFail":"Fail","FLTagID":"22","FLDepth":"22","FLNotes":"22","FLFail":"Fail","RRTagID":"33","RRDepth":"33","RRNotes":"33","RRFail":"Fail","RLTagID":"44","RLDepth":"44","RLNotes":"44","RLFail":"Fail"},{"ID":0,"driverID":"2","event":"Silverstone 12\/3\/2015","race":"Race4","FRTagID":"22","FRDepth":"22","FRNotes":"22","FRFail":"Fail","FLTagID":"33","FLDepth":"33","FLNotes":"33","FLFail":"Fail","RRTagID":"44","RRDepth":"44","RRNotes":"44","RRFail":"Fail","RLTagID":"55","RLDepth":"55","RLNotes":"55","RLFail":"Fail"}]
所以我假设我在PHP中有一个正确形成的数组。
但是我无法使用以下变体回显各个字段:
echo 'driverID ='.$jsonScrutineeringArray2['driverID'][$ii];
我在这里缺少什么?
感谢。
更新。如果我使用var_dump($ jsonScrutineeringArray2);
我明白了:
array(3) {
[0]=>
object(stdClass)#2 (20) {
["ID"]=>
int(1)
["driverID"]=>
string(1) "1"
["event"]=>
string(0) ""
["race"]=>
string(0) ""
["FRTagID"]=>
string(0) ""
["FRDepth"]=>
string(0) ""
["FRNotes"]=>
string(0) ""
["FRFail"]=>
string(0) ""
["FLTagID"]=>
string(0) ""
["FLDepth"]=>
string(0) ""
["FLNotes"]=>
string(0) ""
["FLFail"]=>
string(0) ""
["RRTagID"]=>
string(0) ""
["RRDepth"]=>
string(0) ""
["RRNotes"]=>
string(0) ""
["RRFail"]=>
string(0) ""
["RLTagID"]=>
string(0) ""
["RLDepth"]=>
string(0) ""
["RLNotes"]=>
string(0) ""
["RLFail"]=>
string(0) ""
}
[1]=>
object(stdClass)#3 (20) {
["ID"]=>
int(1)
["driverID"]=>
string(1) "1"
["event"]=>
string(21) "Silverstone 21/2/2015"
["race"]=>
string(5) "Race2"
["FRTagID"]=>
string(2) "11"
["FRDepth"]=>
string(0) ""
["FRNotes"]=>
string(0) ""
["FRFail"]=>
string(4) "Fail"
["FLTagID"]=>
string(3) "111"
["FLDepth"]=>
string(0) ""
["FLNotes"]=>
string(0) ""
["FLFail"]=>
string(4) "Fail"
["RRTagID"]=>
string(4) "1111"
["RRDepth"]=>
string(0) ""
["RRNotes"]=>
string(0) ""
["RRFail"]=>
string(4) "Fail"
["RLTagID"]=>
string(5) "11111"
["RLDepth"]=>
string(0) ""
["RLNotes"]=>
string(0) ""
["RLFail"]=>
string(4) "Fail"
}
[2]=>
object(stdClass)#4 (20) {
["ID"]=>
int(2)
["driverID"]=>
string(1) "2"
["event"]=>
string(21) "Silverstone 12/3/2015"
["race"]=>
string(5) "Race3"
["FRTagID"]=>
string(2) "22"
["FRDepth"]=>
string(0) ""
["FRNotes"]=>
string(0) ""
["FRFail"]=>
string(4) "Fail"
["FLTagID"]=>
string(3) "222"
["FLDepth"]=>
string(0) ""
["FLNotes"]=>
string(0) ""
["FLFail"]=>
string(4) "Fail"
["RRTagID"]=>
string(4) "2222"
["RRDepth"]=>
string(0) ""
["RRNotes"]=>
string(0) ""
["RRFail"]=>
string(4) "Fail"
["RLTagID"]=>
string(5) "22222"
["RLDepth"]=>
string(0) ""
["RLNotes"]=>
string(0) ""
["RLFail"]=>
string(4) "Fail"
}
}
那我该怎么做呢?
答案 0 :(得分:1)
您可以尝试
foreach ($jsonScrutineeringArray2 as $jsonScrutineering) {
echo 'Driver ID = ' . $jsonScrutineering->driverID;
}
答案 1 :(得分:0)
而不是
echo 'driverID ='.$jsonScrutineeringArray2['driverID'][$ii];
试试这个:
echo 'driverID ='.$jsonScrutineeringArray2[$ii]->driverID;
答案 2 :(得分:0)
json_decode($json, $assoc = false)
默认将JS哈希解析为标准对象
$jsonScrutineeringArray2[$ii]->driverID
设置第二个参数以请求PHP关联数组
$jsonScrutineeringArray2[$ii]['driverID']
你在按键顺序中也犯了一个错误
echo 'driverID ='.$jsonScrutineeringArray2[$ii]['driverID'];
而不是
echo 'driverID ='.$jsonScrutineeringArray2['driverID'][$ii];