Jump to content

Fun with Arrays.


kreut

Recommended Posts

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.

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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']);

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.