Jump to content

associative array problems


dbeer

Recommended Posts

I have a very large associative array used to output data from a database.  All of the mySQL queries are working properly, so I didn't post this in that forum. 

I am outputting data obtained from a survey, so not all of the questions are always asked in the same order.  In order to correct this, I have an iterator variable for every question.  Sometimes the associative key for an array is:

home_link_time_lesson_i:0 or something like that. 

When I try to put the values in after adding the key to the array, it claims that the array key doesn't exist.  To be sure, I check if the key is there:

print_r( array_keys($fields) );

and it is there.  Any suggestions for how to tackle this problem?

many thanks

David

Link to comment
https://forums.phpfreaks.com/topic/66532-associative-array-problems/
Share on other sites

I had an idea as I was writing this - I decided to view the array keys in the associative array after I had tried to add the new values to it.  I used the same code:

print_r( array_keys( $fields) );

 

[22] => home_link_follow_up_lesson_i:0

[485] => home_link_follow_up_lesson_i:0

 

Does anyone know why this could happen?  I wonder if the data source is the problem?  The data source is from a mySQL database?

I'm not sure which of the code you want, here is what I think might help:

Originally I add the new associative key/value with the add_to_fields function

 

function add_to_fields(&$fields, $q, $iterators) {

if ($q->type == 'checkbox') {

foreach ($q->options as $opt) {

$f = new field;

$f->is_checkbox = true;

$f->qid = $q->qid;

$f->opt_val = $opt->value;

$f->iterators = $iterators;

$str = $f->to_str();

$fields[$str] = $f;

}

} else {

$f = new field;

$f->qid = $q->qid;

$f->iterators = $iterators;

$str = $f->to_str();

$fields[$str] = $f;

}

 

The important part from that is that it uses the to_str() function to produce the key

to_str() just calls make_str()

 

function make_str($qid, $opt_val, $iterator) {

$str = $qid;

if(strlen($opt_val)){

$str .= '_' . $opt_val;

}

if(strlen($iterator)) {

$str .= '_' . $iterator;

}

return $str;

}

 

Then, below, when I am getting the responses for each question:

 

$qid = (string)mysql_result($responses, $j, 'qid');

$response = (string)mysql_result($responses, $j, 'response');

$iterators = (string)mysql_result($responses, $j, 'iterators');

$opt_val = (string)mysql_result($responses, $j, 'value');

$str = make_str($qid, $opt_val, $iterators);

 

if(strlen($opt_val)){

$fields[$str]->value = 1;

}

else {

$fields[$str]->value = $response;

}

 

and then to print it out:

 

foreach ($fields as $f) {

$table->cell($f->value);

$f->value = '';

}

 

I am getting extra cells because instead of realizing that the key already exists, it is adding duplicate keys to the array, as shown in my last post.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.