doubledee Posted April 16, 2012 Share Posted April 16, 2012 I want to store... - questionID - questionNo - questionText ...in one array? How do I do that?! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/261009-how-do-i-store-3-values-in-an-array/ Share on other sites More sharing options...
gizmola Posted April 16, 2012 Share Posted April 16, 2012 $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. Quote Link to comment https://forums.phpfreaks.com/topic/261009-how-do-i-store-3-values-in-an-array/#findComment-1337697 Share on other sites More sharing options...
l0gic Posted April 16, 2012 Share Posted April 16, 2012 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" ?> Quote Link to comment https://forums.phpfreaks.com/topic/261009-how-do-i-store-3-values-in-an-array/#findComment-1337699 Share on other sites More sharing options...
doubledee Posted April 16, 2012 Author Share Posted April 16, 2012 $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 Quote Link to comment https://forums.phpfreaks.com/topic/261009-how-do-i-store-3-values-in-an-array/#findComment-1337703 Share on other sites More sharing options...
KevinM1 Posted April 16, 2012 Share Posted April 16, 2012 So, do what gizmola suggested 10 times/in a loop. Quote Link to comment https://forums.phpfreaks.com/topic/261009-how-do-i-store-3-values-in-an-array/#findComment-1337708 Share on other sites More sharing options...
gizmola Posted April 16, 2012 Share Posted April 16, 2012 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']} "; } Quote Link to comment https://forums.phpfreaks.com/topic/261009-how-do-i-store-3-values-in-an-array/#findComment-1337710 Share on other sites More sharing options...
doubledee Posted April 16, 2012 Author Share Posted April 16, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261009-how-do-i-store-3-values-in-an-array/#findComment-1337719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.