Adapt script to 2 dimensional array
Posted: Thu Mar 11, 2004 11:16 pm
I am trying to adapt this script to work with a 2 dimensional array
this will give an array indicating the number of variants (e.g. a,b,c) by questions(1,2,3) e.g. question one has two variants, question two has four variants etc
Now that i need to adapt it to use with a 2 dimensional array with a data structure that looks like this
it is the questionId value that I need to assess.
Please assist with this if you can.
Thanks
Code: Select all
$questionBank = array('1a', '1b', '2a', '2b', '2c', '2d', '3a', '3b', '3c', '4a', '4b', '5a', '5b', '5c', '5d', '6a', '6b');
foreach ($questionBank as $numVariants) {
// Access the 1st character of the string
$qIndex = $numVariants[0];
// For every unique question (qIndex)Check to see check to see
// the number of variants available.
// Increment the value of $variantsPerQ for each variant found
if (isset($variantsPerQ[$qIndex])) {
$variantsPerQ[$qIndex]++;
} else {
$variantsPerQ[$qIndex] = 1;
}
}this will give an array indicating the number of variants (e.g. a,b,c) by questions(1,2,3) e.g. question one has two variants, question two has four variants etc
Code: Select all
Array
(
ї1] => 2
ї2] => 4
ї3] => 3
ї4] => 2
ї5] => 4
ї6] => 2
)Code: Select all
Array
(
ї0] => Array
(
їquestionId] => 4a
їquestion] => Which of these country has the most number of inhabitants?
їdistractor1] => Chile
їdistractor2] => Brazil
їdistractor3] => Morocco
їanswer] => China
)
ї1] => Array
(
їquestionId] => 5a
їquestion] => Which of these continents does not have a desert?
їdistractor1] => North America
їdistractor2] => Africa
їdistractor3] => Asia
їanswer] => Europe
)
...Please assist with this if you can.
Thanks