canabatz Posted December 16, 2009 Share Posted December 16, 2009 hi all ,i want to build something like that: $try[] = array( "1", "2", "3"); $try[] = array( "A", "B", "C"); then i want to shffle the first array , so it can be 1,2,3 or ,3,1,2 or 2,1,3 ,any combination possible. then i want to have a form so i can submit the result like that if i see the first array is 1,3,2 then i need to type in my input field ACB and the answer i get is correct ,if the my input is wrong then i get wrong answer . so : 1,2,3 = a,b,c 3,1,2 = c,a,b 2,3,1 = b,c,1 one more thing , a is not equal to 1 ,a is equal to the possition of 1 ,so it can be 1,2,3 for the first array and g,w,r for the second array! my quastion is how im doing something like that , how im tellin the first array that 1 is equal to the position of a and 2 is equal to the position of b ..... and do it in the right way. thanx Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted December 16, 2009 Share Posted December 16, 2009 Simpler to have 1 array rather than a multi-dimensional array i.e <?php $array = range('A','C'); $keys = array_keys($array); shuffle($keys); // display the shuffled array keys print_r($keys); // use the keys to restore the string for($x = 0; $x < count($keys); $x++) { print $array[$keys[$x]]; } ?> Quote Link to comment Share on other sites More sharing options...
bowett Posted December 16, 2009 Share Posted December 16, 2009 Part 1: PHP has a built in shuffle method for arrays: http://php.net/manual/en/function.shuffle.php Part 2: You can extract data from an array using a number: $my_array[4] would return the 5th element in the array. Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 16, 2009 Author Share Posted December 16, 2009 thanks bowett ,i didnt know that i can get the array like that how im doing that : if the array is like that: array( 1,2,3,4,2,1,4,6,4,2,1,3) can i print the position of number one? so the output will be array 0 , 5, 10 - this is the position of 1 in the array! thanx Quote Link to comment Share on other sites More sharing options...
trq Posted December 16, 2009 Share Posted December 16, 2009 Take a look at array_keys. Quote Link to comment Share on other sites More sharing options...
JonnoTheDev Posted December 16, 2009 Share Posted December 16, 2009 All the functions mentioned above are used in my original example Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 16, 2009 Author Share Posted December 16, 2009 what is the way i need to do it? i want to have my array like this $array[] = array (1,2,3,4) $array[] = array (a,b,k,g) i want to have a form to ask me to input the result in order the first array will be in shuffle on every refresh of the page! so if i enter my page and the display will show me 2,1,4,3 then i need to type in the form field b,a,g,k and when i press on submit ,i get back answer if my input was correct! if didnt input it in the right order ,then i get nagative answer! thanx Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 16, 2009 Share Posted December 16, 2009 why do you have two arrays won't the keys of array2 will be same as the values as array1 ? Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 16, 2009 Author Share Posted December 16, 2009 im using shuffle($array) on the first array. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 16, 2009 Share Posted December 16, 2009 why just not do it on the second array ? what connection do they both have ? Quote Link to comment Share on other sites More sharing options...
canabatz Posted December 16, 2009 Author Share Posted December 16, 2009 $array[] = array (1,2,3,4) <==== shuffled $array[] = array (a,b,k,g) im entering the page and i get this sequence 2,1,4,3 and i get a question ,what is equal to 1,3 my answer needs to be b,g i need it in a form ! thanks Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 16, 2009 Share Posted December 16, 2009 I hope I got you right this is what I came up with <?php $array1 = array(2,1,4,3); $array2 = array('a','b','c','g'); $array3 = array(); foreach ($array1 as $key) { array_push($array3,$array2[$key-1]); } print_r($array3); ?> Quote Link to comment 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.