Jump to content

Array creation with non-repeat values


Go to solution Solved by requinix,

Recommended Posts

Hi,

I'm trying to make an array containing values that are unique (i.e. the values are not repeated within the array). There seems to be a mistake somewhere in my code below because only one value (the first element of the array) is being stored in the $randommodulearray array. I would appreciate it if someone could explain why. Cheers.

Also, just to confirm, $module == 'all'.
 

if (($module == 'all') || ($module == 'sem1') || ($module == 'sem2'))
{
$modulearray = array("1"=>"Endocrine","2"=>"Renal","3"=>"Genetics","4"=>"GI","5"=>"Neuro","6"=>"EPISTATS");

$randommodulearray = array();

switch ($module)
{
case 'all':
$numberofmodules = '6';
$min = '1';
$max = '6';
break;

case 'sem1':
$numberofmodules = '3';
$min = '3';
$max = '6';
break;

case 'sem2':
$numberofmodules = '3';
$min = '1';
$max = '3';
break;


}

do
{
$optionmodule = rand($min,$max);
$checkmodule = $modulearray[$optionmodule];
if (in_array($checkmodule, $randommodulearray))
{
    do
    {
        echo 'lol';
        $optionmodule = rand($min,$max);
        $checkmodule = $modulearray[$optionmodule];
        
    }
    while (!in_array($checkmodule, $randommodulearray));
    
$randommodulearray[] = $checkmodule;
}
else
{

$randommodulearray[] = $checkmodule;
}
}
while (count($randommodulearray) == $numberofmodules); // i.e. until they are 6 elements in array


}
Link to comment
https://forums.phpfreaks.com/topic/279059-array-creation-with-non-repeat-values/
Share on other sites

  • Solution

Neater? I don't see how that's neater. But after reading the code I wouldn't suggest using array_unique() anyways - wrong tool for the job.

 

What you have is a bit... off. The main problem is how you're doing the randomization. It's a very inefficient approach and there's something much better.

Try

if (in_array($module, array('all', 'sem1', 'sem2')))
{
	$modulearray = array(
		1 => 'Endocrine',
		2 => 'Renal',
		3 => 'Genetics',
		4 => 'GI',
		5 => 'Neuro',
		6 => 'EPISTATS'
	);

	switch ($module)
	{
		case 'all':
			$modules = array(1, 2, 3, 4, 5, 6);
			break;
		case 'sem1':
			$modules = array(4, 5, 6);
			break;
		case 'sem2':
			$modules = array(1, 2, 3);
			break;
	}

	shuffle($modules);

	$randommodulearray = array();
	foreach ($modules as $m)
	{
		$randommodulearray[] = $modulearray[$m];
	}
}
But to answer the question,

while (count($randommodulearray) == $numberofmodules)
Think about exactly what's happening there. What will happen after $randommodulearray has its first item?

Ahhh, I never knew of the shuffle function. Very useful in this scenerio indeed.

 


while (count($randommodulearray) == $numberofmodules)
Think about exactly what's happening there. What will happen after $randommodulearray has its first item?

 

It should have been:

while (count($randommodulearray) != $numberofmodules)

Thanks for your help.

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.