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
https://forums.phpfreaks.com/topic/227866-fun-with-arrays/
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
https://forums.phpfreaks.com/topic/227866-fun-with-arrays/#findComment-1175041
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
https://forums.phpfreaks.com/topic/227866-fun-with-arrays/#findComment-1175084
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.