【问题标题】:Add an unknown number of strings to DynamoDB向 DynamoDB 添加未知数量的字符串
【发布时间】:2017-03-10 08:36:57
【问题描述】:

在我的 PHP 脚本中,我从 HTML 表单中获得了 POST 响应。其中一个文本是事物列表,以逗号分隔(例如:"apple, banana, orange")。

我想上传此文本以及其他已发布到 Amazon DynamoDB 上的表的文本,但我不想将其作为字符串上传,而是作为字符串集上传(例如:["apple", "banana", "orange"]) .

现在,我知道如何拆分字符串了:

$list = "apple, banana, orange";
$fruits = explode(", ", $list);
echo $fruits[0]; // apple
echo $fruits[1]; // banana
echo $fruits[2]; // orange

而且,我还知道如何将字符串数组上传到 DynamoDB:

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'
]);

$dynamodb = $sdk->createDynamoDb();

$response = $dynamodb->putItem([
    'TableName' => 'ProductCatalog',
    'Item' => [
        'Id'       => ['N'      => '104'      ], // Primary Key
        'Title'    => ['S'      => 'Book 104 Title' ],
        'ISBN'     => ['S'      => '111-1111111111' ],
        'Price'    => ['N'      => '25' ],
        'Authors'  => ['SS'  => [$authorA, $authorB] ]
                ]
]);

但是,如果我不知道确切的数字,如何上传字符串?

如果我知道项目的数量,那就是:

'Fruits' => ['SS' => [$fruits[0], $fruits[1], $fruits[2]]

但问题是我不知道。
尽管我不知道数组中字符串的确切数量,但如何将字符串数组上传到 DynamoDB?

【问题讨论】:

  • 你确定这行不通吗:'Fruits' => ['SS' => $fruits]?
  • @xtx 我不知道。我在 AWS 网站上看到了这段代码。我现在去看看。

标签: php arrays string numbers amazon-dynamodb


【解决方案1】:

您可以直接使用$fruits 作为值,如下:

 $response = $dynamodb->putItem([
 ....
 ....
 'Fruits' => ['SS' => $fruits]

已经是一个数组,所以会自动存储为document/json结构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多