jandrews3 Posted January 20, 2014 Share Posted January 20, 2014 I can't figure out why my shuffle function is behaving the way it is. I've populated a multi-dimensional array $_SESSION['data']['question'][$count], $_SESSION['data']['answer'][$count], and $_SESSION['data']['type'][$count]. When I run the code without the shuffle function, it works properly by printing the question, answer and type data to the screen. But when I use the shuffle() function, my printout to the screen is blank. Thanks. Here is my code: // Get and Assign Questions and Answers $counter = 0; $query3 = "SELECT * FROM inno_quiz_questions WHERE quiz_id = '".$row2['id']."' ORDER BY id"; $result3= mysql_query($query3) or die("Could not perform query: ".mysql_error()); $qnum = mysql_num_rows($result3); while ($row3 = mysql_fetch_array($result3)){ $_SESSION['data']['question'][$counter] = $row3['question']; $_SESSION['data']['answer'][$counter] = $row3['answer']; $_SESSION['data']['type'][$counter] = $row3['type']; $counter++; } shuffle($_SESSION['data']); // Shuffle array $count = 0; while ($count < $_SESSION['quiz']['assigned']){ print $count.". ".$_SESSION['data']['question'][$count]."<br>"; print $_SESSION['data']['answer'][$count]."<br>"; print $_SESSION['data']['type'][$count]."<br><br>"; $count++; } Quote Link to comment https://forums.phpfreaks.com/topic/285529-shuffle-an-array/ Share on other sites More sharing options...
Solution requinix Posted January 20, 2014 Solution Share Posted January 20, 2014 You're shuffling the arrays in $_SESSION['data']. That is, you're shuffling "question" and "answer" and "type" around amongst themselves. You aren't actually shuffling the questions, answers, or types. You need to put each question+answer+type tuple into an array and put that array into the session as a unit. Then you can shuffle it. while ($row3 = mysql_fetch_array($result3)) { $_SESSION['data'][] = $row3; // as the key/values are identical } print $count . ". " . $_SESSION['data'][$count]['question'] . "<br>"; print $_SESSION['data'][$count]['answer']; print $_SESSION['data'][$count]['type']; Quote Link to comment https://forums.phpfreaks.com/topic/285529-shuffle-an-array/#findComment-1465892 Share on other sites More sharing options...
jandrews3 Posted January 20, 2014 Author Share Posted January 20, 2014 Thank you! Works great. I should have seen that. Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/285529-shuffle-an-array/#findComment-1465895 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.