情况如下:
我在PHP中有这个数组,用于保存查询结果中的数据:
$returnData = array(
'ID' => '',
'NAME' => '',
'DESCRIPTION' => '',
'STATUS' => '',
'STATUS_DESCRIPTION' => '',
'LOCATION' => '',
'_ERROR' => ''
);
我稍后执行一个查询,我想填充这个数组:
$fetchResourceQuery = sprintf('SELECT RESOURCEID, RESOURCENAME, RESOURCEDESCRIPTION,
T3.RESOURCELOCATIONNAME, T2.RESOURCESTATUSNAME,
T2.RESOURCESTATUSDESCRIPTION
FROM resource T1
JOIN resource_status T2
ON T1.RESOURCESTATUSID = T2.RESOURCESTATUSID
JOIN resource_location T3
ON T1.RESOURCELOCATIONID = T3.RESOURCELOCATIONID');
$resultSet = $DB->query($fetchResourceQuery);
if($resultSet){
while($row = $resultSet->fetch_assoc()){
$returnData['ID'] = $row['RESOURCEID'];
$returnData['NAME'] = $row['RESOURCENAME'];
$returnData['DESCRIPTION'] = $row['RESOURCEDESCRIPTION'];
$returnData['STATUS'] = $row['RESOURCESTATUSNAME'];
$returnData['STATUS_DESCRIPTION'] = $row['RESOURCESTATUSDESCRIPTION'];
$returnData['LOCATION'] = $row['RESOURCELOCATIONNAME'];
}
$json_data = json_encode($returnData, JSON_UNESCAPED_SLASHES);
echo $json_data;
}
到目前为止这很好。问题是,我认为在保存数据的同时只有一个循环,然后由于某种原因覆盖在数组内部,输出是这样的:
{"ID":"456","NAME":"Rack con Televisor #1","DESCRIPTION":"Televisor Sharp Aquos con Laptop, armado en 2011.","STATUS":"Active","STATUS_DESCRIPTION":"Can be reserved.","LOCATION":"First Floor, High School","_ERROR":""}
这很糟糕,因为数据库中有5个资源,只存储其中一个资源。
如何使数组存储多个值,如josn对象?显然,我现在正在努力的灵魂不起作用。
答案 0 :(得分:3)
$save = array();
while($row = $resultSet->fetch_assoc()){
$returnData['ID'] = $row['RESOURCEID'];
$returnData['NAME'] = $row['RESOURCENAME'];
$returnData['DESCRIPTION'] = $row['RESOURCEDESCRIPTION'];
$returnData['STATUS'] = $row['RESOURCESTATUSNAME'];
$returnData['STATUS_DESCRIPTION'] = $row['RESOURCESTATUSDESCRIPTION'];
$returnData['LOCATION'] = $row['RESOURCELOCATIONNAME'];
array_push($save, $returnData);
}
$json_data = json_encode($save, JSON_UNESCAPED_SLASHES);
echo $json_data;
答案 1 :(得分:2)
您确实在每次迭代时覆盖相同的对象。你想要的是这样的:
$returnData = array(); // Make it an array
// ...
// ...
while($row = $resultSet->fetch_assoc()){
$item = array();
$item['ID'] = $row['RESOURCEID'];
$item['NAME'] = $row['RESOURCENAME'];
$item['DESCRIPTION'] = $row['RESOURCEDESCRIPTION'];
$item['STATUS'] = $row['RESOURCESTATUSNAME'];
$item['STATUS_DESCRIPTION'] = $row['RESOURCESTATUSDESCRIPTION'];
$item['LOCATION'] = $row['RESOURCELOCATIONNAME'];
$returnData[] = $item; // Add new item to array
}
答案 2 :(得分:1)
使用此:
$returnData = array(); // Simply do this. Don't need all index fields
$index = 0;
while($row = $resultSet->fetch_assoc()){
$returnData[$index] = array("ID"=>$row['RESOURCEID'],
"NAME"=>$row['RESOURCENAME'],"DESCRIPTION"=>$row['RESOURCEDESCRIPTION'],
"STATUS"=>$row['RESOURCESTATUSNAME'],"STATUS_DESCRIPTION"=>$row['RESOURCESTATUSDESCRIPTION'],
"LOCATION"=>$row['RESOURCELOCATIONNAME']);
$index++;
}
echo json_encode($returnData, JSON_UNESCAPED_SLASHES);
答案 3 :(得分:0)
$newArray = array();
while($row = $resultSet->fetch_assoc()){
$returnData['ID'] = $row['RESOURCEID'];
$returnData['NAME'] = $row['RESOURCENAME'];
$returnData['DESCRIPTION'] = $row['RESOURCEDESCRIPTION'];
$returnData['STATUS'] = $row['RESOURCESTATUSNAME'];
$returnData['STATUS_DESCRIPTION'] = $row['RESOURCESTATUSDESCRIPTION'];
$returnData['LOCATION'] = $row['RESOURCELOCATIONNAME'];
array_push($newArray, $returnData);
}