CodeIgniter Forums
what is an associative array? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: what is an associative array? (/showthread.php?tid=71416)



what is an associative array? - richb201 - 08-12-2018

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.


RE: what is an associative array? - donpwinston - 08-12-2018

Looks like 'Item' => $encoded_data is a json string. You need to use 'Item' => json_decode($encoded_data, true)


RE: what is an associative array? - richb201 - 08-13-2018

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".


RE: what is an associative array? - Pertti - 08-13-2018

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?


RE: what is an associative array? - richb201 - 08-13-2018

I am actually on 5.6.28.  I found this article: https://aws.amazon.com/blogs/developer/dynamodb-json-and-array-marshaling-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."


RE: what is an associative array? - richb201 - 08-13-2018

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?


RE: what is an associative array? - richb201 - 08-13-2018

Could I use

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

and replace "" with the word "null"?


RE: what is an associative array? - Pertti - 08-13-2018

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




RE: what is an associative array? - richb201 - 08-13-2018

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.


RE: what is an associative array? - richb201 - 08-13-2018

That seemed to works. thx