CodeIgniter Forums

Full Version: what is an associative array?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
Looks like 'Item' => $encoded_data is a json string. You need to use 'Item' => json_decode($encoded_data, true)
I was actually already doing that (without the true)


{"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".
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?
I am actually on 5.6.28.  I found this article:

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();
   $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."
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?
Could I use

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

and replace "" with the word "null"?
Maybe simple script like this will work:
PHP Code:
foreach ($data as $index => $value) {
    if (!
$value) {

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.
That seemed to works. thx