Jump to content

How do I store 3 values in an Array?


doubledee
 Share

Recommended Posts

You'll want a multi-dimensional array!

 

<?php
$questions = array( array("Question One", "Question One Text"),  // id would be like $questions[0][*]
                    array("Question Two", "Question Two Text"),
                    array("Question Three", "Question Three Text"),
                   ); 
?>

 

So..

 

<?php
echo $questions[0][0]; // would echo "Question One"
echo $questions[0][1]; // would echo "Question One Text"

echo $questions[1][0]; // would echo "Question Two"
echo $questions[1][1]; // would echo "Question Two Text"
?>

Link to comment
Share on other sites

$questions[] = array('questioniID' => 3, 'questionNo' => 5, 'questionText' => 'This is question 1');

 

This assumes of that you will have multiple questions, each of which you'd assign in a similar fashion.

 

I have 10 Questions, and for each Question I want to store: questionID, questionNo, questionText

 

Like this....

ID, No, Text
2, 1, Why did you start your own business?
5, 2, What would you recommend that others make sure and do?
6, 3, What would you recommend that others make sure and NOT do?
:
14, 10, How do you compete with large corporations?

 

 

Debbie

Link to comment
Share on other sites

Yes, just repeat for each question.  It is also possible to do this all in one big static definition, but might be harder to maintain, when you want to switch the order of questions, or edit them.

 

Using your example:

 

$questions[] = array('questioniID' => 2, 'questionNo' => 1, 'questionText' => 'Why did you start your own business?');
$questions[] = array('questioniID' => 5, 'questionNo' => 2, 'questionText' => 'What would you recommend that others make sure and do?');
//etc

foreach ($questions as $question) {
  echo "{$question['questionText']} 
";
}

 

I expect that what you're actually planning to do is display the questions in questionNo order, so what I'd probably do instead is this:

 


$questions[1] = array('questioniID' => 2, 'questionText' => 'Why did you start your own business?');
$questions[2] = array('questioniID' => 5, 'questionText' => 'What would you recommend that others make sure and do?');
//etc

 

Now your questions are automatically ordered:

 

foreach ($questions as $nbr => $question) {
   echo "$nbr. {$question['questionText']} 
";
}

 

 

 

Link to comment
Share on other sites

gizmola,

 

Excellent answer, but I think what I need to figure out first is a related thread here...

 

http://www.phpfreaks.com/forums/index.php?topic=357782.new;topicseen#new

 

(I didn't mean to repeat myself, but these are two different, yet related threads.)

 

I think I need to figure out a strategy on the Database side first, and then decide if I want to use Arrays like you are showing me here.

 

Please let me know what you think over on this other thread as well!

 

Thanks,

 

 

Debbie

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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