Reputation: 2492
I have a PHP script, and in this script I have an associative array looks like this, so I have pairs of keys and values in the array:
$arr = array('First' => 'One', 'Second' => 'Two', 'Third' => 'Three');
I have a table in AWS DynamoDB, and on that table I have an attribute named details
and its type is Map
, which means it has a String
Key and a String
Value for every item in the Map
, so eventually it looks like the associative array from the beginning of the question.
Now, what I want is to upload the associative array to the table at DynamoDB, to the details
Map-type attribute.
On the DynamoDB PHP SDK Documentation, it is written that I need to upload the Map keys and values pairs manually, like this:
'M' => array(
// Associative array of custom 'AttributeName' key names
'AttributeName' => array(
// Associative array of custom key value pairs
),
// ... repeated
),
But, the problem is that I don't know how many pairs of keys and values are in the associative array, so I cannot split it to 2 arrays of keys and values and to upload it with the number of pairs I have:
$keys[0] => array('S' => $values[0]),
$keys[1] => array('S' => $values[1]),
...
Another option that I thought about was to create a loop, and each time to update the attribute in the DynamoDB table, but the problem is that it is a Map-type attribute, so I cannot use ADD
or SET
to update the map. I cannot also retrieve the exist map on the DB and add the new pair, because then I still stay with a Map-type variable.
I thought of another option, which is to split it to 2 arrays of keys and values and upload those arrays to the DB, but then I would lose the order of the strings in the arrays and the matching between the keys and the pairs, because DynamoDB orders the arrays alphabetically, so there is no match by the index number.
What can I do to upload the associative array to the DynamoDB table?
Upvotes: 1
Views: 2554
Reputation: 54
You can use DynamoDB Marshaler! Class. The Marshaler object has methods for marshaling JSON documents and PHP arrays to the DynamoDB item format and unmarshaling them back.
$data = [
'id' => '5432c69300594',
'name' => [
'first' => 'Jeremy',
'middle' => 'C',
'last' => 'Lindblom',
],
'age' => 30,
'phone_numbers' => [
[
'type' => 'mobile',
'number' => '5555555555',
'preferred' => true
],
[
'type' => 'home',
'number' => '5555555556',
'preferred' => false
],
],
];
// Marshaling the data and putting an item.
$client->putItem([
'TableName' => 'YourTable',
'Item' => $marshaler->marshalItem($data)
]);
// Getting and item and unmarshaling the data.
$result = $client->getItem([
'TableName' => 'YourTable',
'Key' => ['id' => ['S' => '5432c69300594']]
]);
$data = $marshaler->unmarshalItem($result['Item']);
Here is the reference: https://aws.amazon.com/es/blogs/developer/dynamodb-json-and-array-marshaling-for-php/
Upvotes: 3
Reputation: 148
If I understand your question correctly, I think you basically have what you need. You seem to have mistakenly interpreted AttributeName as a literal string rather than the placeholder for the name that you've defined ('details'). I did the same thing myself the first time through.
According to the docs for PutItem, the 'Item' attribute in your API call should look like this.
'Item' => array(
'details' => array(
'M' => array(
'First' => array( 'S' => 'One'),
'Second' => array( 'S' => 'Two'),
'Third' => array( 'S' => 'Three'),
),
),
), etc.
So if you take your original $arr and using a loop or array_map() create a new array $arr2 in that format, you can just assign it:
'Item' => array(
'details' => array( 'M' => $arr2 ),
),
Same would apply for UpdateItem
'ExpressionAttributeNames' => array(
'#d' => 'details'
),
'ExpressionAttributeValues' => array(
':d' => array(
'M' => array(
'First' => array( 'S' => 'One'),
'Second' => array( 'S' => 'Two'),
'Third' => array( 'S' => 'Three')
)
)
),
'UpdateExpression' => 'SET #d = :d',
Upvotes: 2