我正在从数组中将变量插入AWS DynamoDB $ dbf [$ a] ['variable1'],$ dbf [$ a] ['variable2'],$ dbf [$ a] ['variable3'] ...
对于某些$ a,只设置'variable1'和'variable2',并且 对于其他$ a,所有'变量#'都已设置。
以下代码不起作用,因为Null或非set变量不起作用 允许。
循环中是否只有一种方法只能为设置的变量尝试“PutItem”?
foreach($dbf as $day)
{
$result = $client->PutItem(array(
'TableName' => 'AWIS',
'Item' => array(
'id' => array('S' => $day['id']),
'date'=> array('S' => $day['date']),
'max'=> array('N' => $day['max']),
'min'=> array('N' => $day['min']),
'pre'=> array('N' => $day['pre']),
'max_soil_temp'=> array('N' => $day['max_soil_temp']),
'min_soil_temp'=> array('N' => $day['min_soil_temp']),
'evap'=> array('N' => $day['evap']),
'veg_wetting'=> array('N' => $day['veg_wetting']),
'solar_rad'=> array('N' => $day['solar_rad']),
'ob_temp'=> array('N' => $day['ob_temp']),
'adj_min'=> array('N' => $day['adj_min']),
'chill_hours'=> array('N' => $day['chill_hours'])
),
));
}
答案 0 :(得分:1)
您可以这样做:
if ($yourValue) {
$params['yourKey'] = array(
'Action' => 'PUT',
'Value' => array(
Type::STRING => $yourValue
)
);
}
$response = $Client->updateItem(array(
"TableName" => "tableName",
"Key" => array(
"Id" => array(
Type::STRING => $Id
)
),
"AttributeUpdates" => $params
)
);
设置循环中可用的所有参数,并仅更新那些可用的值
希望有所帮助