• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is an associative array?

#1
What is an associative array? I am getting an error when I try assign the following to Item before sending this to the server. The error is "\n[Item] must be an associative array. Found <pre class='xdebug-var-dump' dir='ltr'>\n<small>C:\fullpath to my vendor\aws\src\functions.php:252:</small><small>string</small> <font color = ..."

$params = [
   'TableName' => $tableName,
   'email'=>$admin_email,  //sort
   'year'=> $year,        //partition
   'Item' => $encoded_data


The $encoded_data variable is 

$encoded_data={"survey_compiler":"[email protected]","campaign":"Apple","compiler_relation":"private","admin_email":"[email protected]","year":"2010","employee":"Bandfrom Bossanova","employee_email":"[email protected]","employee_id":"","task1":"adapting to a customers needs","task1_percent":"1","task2":"","task2_percent":"0","task3":"","task3_percent":"0","task4":"","task4_percent":"0","task5":"","task5_percent":"0","task6":"","task6_percent":"0"}


I think that Item is an associative array. What a strange error message.
Reply

#2
Looks like 'Item' => $encoded_data is a json string. You need to use 'Item' => json_decode($encoded_data, true)
Simpler is always better
Reply

#3
I was actually already doing that (without the true)

$encoded_data=json_encode($data,true);

$endcoded_data=
{"survey_compiler":"[email protected]","campaign":"Apple","compiler_relation":"private","admin_email":"[email protected]","year":"2010","employee":"Bandfrom Bossanova","employee_email":"[email protected]","employee_id":"","task1":"","task1_percent":"0","task2":"","task2_percent":"0","task3":"","task3_percent":"0","task4":"","task4_percent":"0","task5":"","task5_percent":"0","task6":"","task6_percent":"0"}

and then

$params = [
'TableName' => $tableName,
'email'=>$admin_email, //sort
'year'=> $year, //partition
'Item' => $encoded_data
];

BTW, when I look at it in the debugger, there is a double quote at the beginning and the end.

same error "must be an associative array".
Reply

#4
To convert object to array, you can do:
PHP Code:
$encoded_data = (array) $encoded_data

Tho json_encode($data, true) should already return array, so there might be something else going on. Apparently it's only available since PHP 5.4.0, could it be you're on older version?
Reply

#5
I am actually on 5.6.28.  I found this article: https://aws.amazon.com/blogs/developer/d...g-for-php/

which talks about getting data from either json or a string into a format acceptable to dynamodb. They have a thing called :

Marshaling a Native PHP Array
The Marshaler also provides the marshalItem() and unmarshalItem() methods that do the same type of thing, but for arrays. This is essentially an upgraded version of the existing DynamoDbClient::formatAttributes() method.

 So if I use my $data array (which has not been made into json), I have

   $dynamodb = $sdk->createDynamoDb();
   $marshaler = new Marshaler();
   $tableName='historical-activities';
   $params = [
       'TableName' => $tableName,
       'Item' => $marshaler->marshalItem($data)
   ];
   $result = $dynamodb->putItem($params);

This gives me Marshalling error: empty strings are invalid. That is true I see a bunch of empty strings above.

Then I did a search on that error and I found:

"I'm sorry, but this means that if you have an object with empty strings, you have to prune them out prior to marshaling. That is obnoxious though, I'll give it more thought. Perhaps it would be okay for the marshaler to be configured to prune/replace empties."
Reply

#6
So the answer might be that I need to go through the $data and change all empty strings to   NULL. Any idea how I would do this?
Reply

#7
Could I use

word_censor($str, $censored[, $replacement = ''])

and replace "" with the word "null"?
Reply

#8
Maybe simple script like this will work:
PHP Code:
foreach ($data as $index => $value) {
    if (!
$value) {
        unset(
$data[$index]);
    }

Reply

#9
Thanks. I am not sure how this works. I guess it is iterating through the associative array which is cool. I am just concerned about the unset. Perhaps I could replace the unset with $data[$index]='null'. I am hoping to use a string "null" rather than the NULL value.
Reply

#10
That seemed to works. thx
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.