zero742 Posted January 6, 2011 Share Posted January 6, 2011 I've been working on some code to: 1. Search a db for rows that have a particular "position" value 2. Search the same db for rows that have a particular "langs" value 3. Compare the two arrays resulting for 1 and 2 4. Create a multidimensional array $langsarray[langs][question id] if 3 is true. 5. For each langs array in $langsarray pick a random, non-duplicate value and add it to $qarray for a maximum of 12 elements (3 for each langs). What I have works about 95%. The problem is, in the first langs array, one of the 3 randomly picked values is almost always empty, and its not always the same element. Sometimes its the first, sometimes its the third, sometimes its the second... I think I've narrowed down the issue to line 44 ($qtemp = ...). If I change the min/max values for the mt_rand function, it behaves slightly differently. Any help is much appreciated! Thanks in advance. Code: //Include MYSQL class and authentication information require_once('./include/mysql.php'); require_once('./include/global.php'); //Grab apptype from URL querystring $apptype = $_GET['apptype']; //Declare arrays $qarray = array(); $posarray = array(); $langs = array(); $langsarray = array(); //Get id's of all questions pertaining to position type $sql = 'SELECT * FROM qa WHERE position = "' . $apptype . '"'; $result = $db->query($sql) or die(mysql_error()); while ($row = $result->fetch()) { $posarray[] = $row['id']; } //Determine what types of questions should be pulled if ($apptype == 'fed'){ $langs = array('html','css','javascript','jquery'); } else if($apptype == 'bed'){ $langs = array('php','asp','javascript','jquery'); } //Get each question of each language that matches the position type and store it in the multidimensional array $langsarray[language][question id] $z = 0; while ($z < count($langs)){ $sql = 'SELECT * FROM qa WHERE langs = "' . $langs[$z] . '"'; $result = $db->query($sql) or die(mysql_error()); while ($row = $result->fetch()) { if (in_array($row['id'],$posarray)){ $langsarray[$langs[$z]][] = $row['id']; } } //Takes a random question id from the current language ($z) and adds it to the final question array. $y = 0; while ($y < 3) { //$qtemp = $langsarray[$langs[$z]][mt_rand(0,count($langsarray[$langs[$z]]))]; $qtemp = $langsarray[$langs[$z]][mt_rand(0,count($posarray))]; if (!in_array($qtemp,$qarray)){ $qarray[] = $qtemp; echo $qtemp . ', '; $y++; } } echo $langs[$z]; print_r( $langsarray[$langs[$z]]); echo '<br />'; $z++; } echo '<br />'; print_r($qarray); Output: 5, , 2, htmlArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) <--Problem 7, 6, 9, cssArray ( [0] => 6 [1] => 7 [2] => 8 [3] => 9 [4] => 10 ) 14, 15, 13, javascriptArray ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 [4] => 15 ) 21, 17, 20, jqueryArray ( [0] => 16 [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => 21 ) Array ( [0] => 5 [1] => [2] => 2 [3] => 7 [4] => 6 [5] => 9 [6] => 14 [7] => 15 [8] => 13 [9] => 21 [10] => 17 [11] => 20 ) <-- Final $qarray Link to comment https://forums.phpfreaks.com/topic/223617-multidimensional-array-help/ Share on other sites More sharing options...
BlueSkyIS Posted January 6, 2011 Share Posted January 6, 2011 without setting up a db, etc., etc., etc., i suspect that you want that line to be $qtemp = $langsarray[$langs[$z]][mt_rand(0,count($posarray) - 1)]; but maybe not... Link to comment https://forums.phpfreaks.com/topic/223617-multidimensional-array-help/#findComment-1155902 Share on other sites More sharing options...
zero742 Posted January 6, 2011 Author Share Posted January 6, 2011 without setting up a db, etc., etc., etc., i suspect that you want that line to be $qtemp = $langsarray[$langs[$z]][mt_rand(0,count($posarray) - 1)]; but maybe not... Tried... but no I can provide the Create Table SQL code and some dummy data if that would be helpful. Link to comment https://forums.phpfreaks.com/topic/223617-multidimensional-array-help/#findComment-1155907 Share on other sites More sharing options...
zero742 Posted January 7, 2011 Author Share Posted January 7, 2011 Fixed the problem this morning. Apparently there is a bug in the mt_rand function that after a certain number of iterations, the function gets "stuck" and produces the same result over and over. It is intermittent and therefore never gets stuck in the same spot or on the same number. As soon as I replaced mt_rand with array_rand (which is probably a better choice anyway...) the problem was immediately solved. Bazinga! Link to comment https://forums.phpfreaks.com/topic/223617-multidimensional-array-help/#findComment-1156250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.