Jump to content

Recommended Posts

I still hate arrays! 🙁

Could use some help expanding an array by adding another dimension.  (I'm sure this will be super simple for the gurus here!)

Currently I have an array that holds my questionnaire questions (HMTL-formt) like this...

questionnaireArray
   array
      1 =>    <li>Question-1 here</li>
      2 =>    <li>Question-2 here</li>
      3 =>    <li>Question-3 here</li>
      4 =>    <li>Question-4 here</li>
	

 

I want to expand this PHP page so that a series of questions are part of a "Section" that may (or may not) contain some lead-in text.  (The idea being that sometimes it is necessary to have a short intro to a series of questions, and that text is better placed next to the questions, as opposed to at the top if the page.)

 

The end result I want would look like this...

	QUESTIONNARIE
    
	<Intro Synopsis>
    
	<section-1 intro is blank>
	Q1.)
	Q2.)
	 
	<section-2 intro = "Some text here...">
	Q3.)
	Q4.)
	 
	<section-3 intro = "Some more text here...">
	Q5.)
	Q6.)
	

 

So I could use help taking my current array (above), and nesting it within a parent array that will contain each Section's intro text - which could be blank.

And to be clear, what I need help with now is *visualizing* what the new array will look like - versus how to create it.  

(Once I understand what it should look like and what the keys and values are, then i can probably figure out the PHP coding part!)

 

For instance, what is the $key for my parent array? 

What are the $values for the parent array?

And how does my current array (above) fit into this?

 

Thanks!

 

 

Edited by SaranacLake
Link to comment
https://forums.phpfreaks.com/topic/312327-need-help-expanding-an-array/
Share on other sites

  1. Every element in an array has a key and a value
  2. A value may itself be an array

So your would probably look something like this

$questionnaire = [
                     'title'  => 'questionnaire one',
                     'intro'  => 'Synopsis goes here',
                     'sections'  => [
                                        '1'  =>  [
                                                     'intro' => '',
                                                     'questions' =>  [
                                                                         '1'  =>  'question 1 text',
                                                                         '2'  =>  'question 2 text'
                                                                     ]
                                                 ],
                                                 
                                        '2'  =>  [
                                                     'intro' => 'Some text here',
                                                     'questions' =>  [
                                                                         '3'  =>  'question 3 text',
                                                                         '4'  =>  'question 4 text'
                                                                     ]
                                                 ]
                                    ]
                 ];

which could come from data like this

+---------------------+
| questionnaire       |
+---------------------+
| quaire_id           |-----+
| title               |     |
| intro               |     |
+---------------------+     |        +-----------------+
                            |        | section         |
                            |        +-----------------+
                            |        | section_id      |------+
                            +-------<| quaire_id       |      |
                                     | section_no      |      |
                                     | intro           |      |
                                     +-----------------+      |           +-------------------+
                                                              |           | question          |
                                                              |           +-------------------+
                                                              |           |  q_id             |
                                                              +----------<|  section_id       |
                                                                          |  q_number         |
                                                                          |  q_text           |
                                                                          +-------------------+

 

  • Like 1

There's LOTS of stuff online about arrays. And you aren't going to do better than Barand.

If you've already got an established array, you might try using print_r() [there are online resources to explain it's functionality]. This will give you a visual representation that may help you understand the arrays design.

Don't forget that arrays begin with zero (ie: my_array[1] is the SECOND item).

The first item WITHIN my_array[1] would be found by using

my_array[1][0]

Finding the third item in an assortment within my_array[1][0] would be accomplished with 

my_array[1][0][2]

Etc, etc, etc.

Or, if you hate arrays, why not use a variable for each question?

$question1 = "who";

$question2 = "when";

$question2a = "when in morning";

$question2b = "when at night";

$question3.….. etc.

11 hours ago, phppup said:

Don't forget that arrays begin with zero (ie: my_array[1] is the SECOND item).

True by default when no keys specified.

$letters = ['a', 'b', 'c'];
echo $letters[0];                           //--> 'a'
echo $letters[1];                           //--> 'b'
echo $letters[2];                           //--> 'c'

// but

$months = [ 1 => 'Jan', 'Feb', 'Mar' ];
echo $months[0];                            //-> Notice: Undefined offset: 0
echo $months[1];                            //-> 'Jan'
echo $months[2];                            //-> 'Feb'

 

@Barand

Sorry for the late response, but I have been tied up with another part of my website for the last week or so....

Thanks for the sample array above!

I am trying to get my head back into things, and am sure I will have follow-up questions.

Q1.) For starters, in your multi-dimensional array above, 'title', 'intro', and 'sections' are [keys[/b] for the outermost array, right?

 

Q2.) I forgot, what do you call arrays where the keys are text values like your example?

Associative arrays?

 

Q2b.) And what do you call arrays where the keys are numeric?

 

Q3.) I assume that text keys cannot/should not have spaces in them like 'first name', right?

 

 

1 ) the bit before the => is the key, the bit following the => is the value.

2 ) Yes, associative

2b) indexed, or numeric, arrays

2c) But they can be mixed, for example

$arr = [ 'This is a key' => 'This is a value',
         42 => 'second value',
         'Third value'
       ];
       
echo $arr['This is a key'] . '<br>';
       
echo '<pre>', print_r($arr, 1), '</pre>'; 

which outputs

This is a value
Array
(
    [This is a key] => This is a value
    [42] => second value
    [43] => Third value
)

3 ) I think the above example answers that.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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