Adapt script to 2 dimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Adapt script to 2 dimensional array

Post by jbatty »

I am trying to adapt this script to work with a 2 dimensional array

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
)
Now that i need to adapt it to use with a 2 dimensional array with a data structure that looks like this

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
         )
...
it is the questionId value that I need to assess.
Please assist with this if you can.
Thanks
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: Adapt script to 2 dimensional array

Post by TheBentinel.com »

How about something on the order of:

Code: Select all

$questionBank = [your new array definition];
foreach ($questionBank as $numVariantsCollection) {
        $numVariants = $numVariantsCollection["questionId"];
        // 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;
        } 
    }


Does that work? Shooting from the hip here, I don't have much experience with multi-dimensional arrays.
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Post by jbatty »

Thank you, thebentinel.com, it worked.
Post Reply