1

In my PHP script, I get a POST reaction from an HTML form. One of the texts, is a list of things, separated by a comma (for example: "apple, banana, orange").

I want to upload this text, among with other texts which got POSTed to a table on Amazon DynamoDB, but I don't want to upload it as a string, but as a string set (for example: ["apple", "banana", "orange"]).

Now, I know how to split the string:

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

And, I also know how to upload a string array to 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] ]
                ]
]);

But, how to upload the strings, if I don't know the exact number?

If I knew the number of items then it would be like:

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

But the problem is that I don't know.
How can I upload the string array to DynamoDB although I don't know the exact number of strings in the array?

2
  • 1
    Are you sure this won't work: 'Fruits' => ['SS' => $fruits]? Commented Mar 10, 2017 at 9:31
  • @xtx I don't know. I saw this code on AWS website. I'll check it now. Commented Mar 10, 2017 at 9:31

1 Answer 1

1

You can directly use $fruits as value as below :

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

It is already an array, So it will be automatically stored as document / json structure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.