I was thinking that I could use a group_concat in an mysql query to create an array on the fly that could be used elsewhere in my script. Say I have a line like this in the query:

GROUP_CONCAT(CHOICE_ID,'=>"',RESPONSE_TEXT,'"') AS 'CHOICE_TEXTS',

...which DOES seem to work, as it gives me the following output (when I use print_r()to show it)

9=>"Yes",10=>"No-transition",11=>"No-disruption",12=>"Assessment"

...but it does not seem to behave as an array when I try to...
echo "<table>";
foreach($line[CHOICE_TEXTS] as $k=>$val) {echo "<tr><td>$k</td><td>$val</td></tr>";}
echo "</table>";

...seems like I'm kind of close here. any hints on how to get this raw string to behave as an array?
9=>"Yes",10=>"No-transition",11=>"No-disruption",12=>"Assessment"

Thanks

    A string that contains a bunch of text with some equal signs and greater-than signs is completely different than an array.

    Sounds like a better approach would be to loop through each result without grouping them together and creating the array on-the-fly as you process each row.

      I figured that might be the case, but thought there might be a simple way to turn one into the other.

      I worked out the following solution which seems to do the trick:

      while ($line=mysqli_fetch_array($formstuff_result, MYSQLI_ASSOC)) {
      $_SESSION[formstuff][$line[ITEMSET_ID]]=$line;
      $tempfull=explode(",",$line[CHOICE_TEXTS]);
      foreach($tempfull as $str) {$parts=explode("=>",$str);
      $CHOICE_TEXTS[$line[ITEMSET_ID]][$parts[0]]=$parts[1];}

      }

        All fields being grouped together by a GROUP BY clause will end up as a comma separated string if you use the GROUP_CONCAT aggregate function. Thus, you could explode(',', the_gc_field) to turn that string into an array.

          Write a Reply...