2015-05-21 88 views
3

我將變量從數組插入到AWS DynamoDB中 $ dbf [$ a] ['variable1'],$ dbf [$ a] ['variable2'] $ dbf [$ a] ['variable3'] ...適用於PHP的AWS開發工具包DynamoDB僅適用於非空變量的PutItem

對於某些$ a,只設置'variable1'和'variable2',對其他$ a設置 ,設置所有'變量#'。

下面的代碼將不起作用,因爲空值或非設置變量不允許爲 。

是否有一種方法在循環中只嘗試爲設置的變量「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']) 
       ), 
          )); 
} 

回答

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 
       ) 
     ); 

設置凡在循環使用,並且可以

希望幫助

將只更新這些值PARAMS
相關問題