MFA Posted June 12, 2013 Share Posted June 12, 2013 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 } Quote Link to comment https://forums.phpfreaks.com/topic/279059-array-creation-with-non-repeat-values/ Share on other sites More sharing options...
requinix Posted June 12, 2013 Share Posted June 12, 2013 Does array_unique suit your needs better than writing something yourself? Quote Link to comment https://forums.phpfreaks.com/topic/279059-array-creation-with-non-repeat-values/#findComment-1435488 Share on other sites More sharing options...
MFA Posted June 12, 2013 Author Share Posted June 12, 2013 I knew I could use the array_unique function but I decided to write something more neat. Do you have any ideas as to why my code doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/279059-array-creation-with-non-repeat-values/#findComment-1435541 Share on other sites More sharing options...
Solution requinix Posted June 12, 2013 Solution Share Posted June 12, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/279059-array-creation-with-non-repeat-values/#findComment-1435581 Share on other sites More sharing options...
MFA Posted June 13, 2013 Author Share Posted June 13, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279059-array-creation-with-non-repeat-values/#findComment-1435636 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.