Jump to content

Multidimensional array help


zero742

Recommended Posts

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
Share on other sites

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
Share on other sites

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!  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.