dbeer Posted August 24, 2007 Share Posted August 24, 2007 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 More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 You should post your code that produces the error. Link to comment https://forums.phpfreaks.com/topic/66532-associative-array-problems/#findComment-333230 Share on other sites More sharing options...
dbeer Posted August 24, 2007 Author Share Posted August 24, 2007 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? Link to comment https://forums.phpfreaks.com/topic/66532-associative-array-problems/#findComment-333235 Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 I don't know what is happening that shouldn't with what you posted. It would be a lot easier if you posted the code that generated te error. Link to comment https://forums.phpfreaks.com/topic/66532-associative-array-problems/#findComment-333239 Share on other sites More sharing options...
dbeer Posted August 24, 2007 Author Share Posted August 24, 2007 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. Link to comment https://forums.phpfreaks.com/topic/66532-associative-array-problems/#findComment-333240 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.