kreut Posted February 16, 2011 Share Posted February 16, 2011 Hello, I have 2 array questions that I'm hoping someone can help me with. 1) First, I'm trying to add elements to a previously existing array. I start with: $data = array('text' => $_POST['text'], 'question_type' => $_POST['question_type'], 'solution' => $_POST['solution'], 'filename' => $filename, 'author_id' => $_POST['author']); Then, under certain circumstances, I'm going to want to add the following to the array $data: 'incorrect_solution1' => $_POST['incorrect_solution1'], 'incorrect_solution2' => $_POST['incorrect_solution2'], 'incorrect_solution3' => $_POST['incorrect_solution3']) I tried the concatenation function, combined with an if statement but then PHP thinks that I have a string. 2) Once I have the array, I want to shuffle the solution and 3 incorrect solution indices. I'm not sure how to shuffle just a portion of an array. Any help with either or both questions would be appreciated. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/227866-fun-with-arrays/ Share on other sites More sharing options...
ansharma Posted February 16, 2011 Share Posted February 16, 2011 use array_merge function $array1 = array('green', 'red', 'yellow'); $array2 = array('avocado', 'apple', 'banana'); $result = array_merge($array1, $array2); print_r($result); also you can use array_push function $stack = array("orange", "banana"); array_push($stack, "apple", "raspberry"); print_r($stack); Quote Link to comment https://forums.phpfreaks.com/topic/227866-fun-with-arrays/#findComment-1175041 Share on other sites More sharing options...
Psycho Posted February 16, 2011 Share Posted February 16, 2011 Rather than adding the incorrect solutions to the "root" of the array, you should add them to a subarray element (for example "answers"). In addition you could add the correct solution (while also maintaining the correct in the root) to this sub array element. Then when you want to show the "possible" solutions just randomize that sub-array and display them. Lastly, don't worry about giving the incorrect solutions unique names - just add them to the array and let it index them. Example array structure array( 'text' => "what is 2 + 2", 'question_type' => "math", 'solution' => "4", 'filename' => "math_test.txt", 'author_id' => 1, 'answers' => array ( 0 => "4", 1 => "6", 2 => "3", 3 => "5" ) ) So, to display the possible solutions in a random order you would just need to randomize $data['answers'] using shuffle($data['answers']); Quote Link to comment https://forums.phpfreaks.com/topic/227866-fun-with-arrays/#findComment-1175084 Share on other sites More sharing options...
kreut Posted February 16, 2011 Author Share Posted February 16, 2011 Thank you both for clear and precise responses! Quote Link to comment https://forums.phpfreaks.com/topic/227866-fun-with-arrays/#findComment-1175137 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.