Jump to content

Multidimensional Array?


Naps

Recommended Posts

I am looking at the code below:

 

$array = array(
                array(
                1, 2
          ),
          'a' => array(
                'b' => 1, 
                'c'
        )
);       

 

Is this an example of a multidimensional array? If so how would I access the value of 1 from key b?

 

Also would the next line ('c') be automatically assigned to the asociative key of c?

Link to comment
Share on other sites

Is this an example of a multidimensional array?

 

yes

 

If so how would I access the value of 1 from key b?

 

$array['a']['b'];

 

Also would the next line ('c') be automatically assigned to the numerical key of c?

 

what?

Link to comment
Share on other sites

*edit

Thanks AyKay47

 

Also would the next line ('c') be automatically assigned to the numerical key of c?

 

what?

 

I meant to say would 'c' be assigned to the aplhabetical key of 'c' or for example '1'

 

'a' => array(                'b' => 1,                 'c'

Link to comment
Share on other sites

Yes, that is a multi-dimension array.  You would access the value 1 of key b using:

$array['a']['b'];

 

The first level you need to get into is key 'a', then you need to go into key 'b' to get the 1.

 

The value 'c' would be assigned to index 0.  When PHP auto-indexes it uses either 0 if there are no existing numeric indexes, or whatever the highest numerical index is, +1 as the new index

Link to comment
Share on other sites

Also would the next line ('c') be automatically assigned to the [edit] alphabetical key of c?

 

That does nothing to clarify your question. :P

 

The only way to assign anything to the key "c" is to explicitly assign something to the key "c".

$array['a]['c'] = 'something';

Link to comment
Share on other sites

Thanks for the further info kicken and scootach. I will try to elaborate.

 

When for example when you create an array:

 

$array = array(1, 2, 3, 4, 5);

 

My understanding is that it’s values will be automatically assigned to the keys as follows:

 

[edit : auto formatted wrong!]

[0] 1
[1] 2
[2] 3
[3] 4
[4] 5

 

I am wondering what happens in that second part of the multidimensional array

 

‘a’ => array(‘b’ => 1, ‘c’)

 

How would the value c be accessed? Or is c a key containing nothing?

 

Link to comment
Share on other sites

My understanding is that it’s values will be automatically assigned to the keys as follows:

 

1

[1] 2

[2] 3

[3] 4

[4] 5

 

value one would be assigned to index 0

 

I am wondering what happens in that second part of the multidimensional array

 

Code: [select]

‘a’ => array(‘b’ => 1, ‘c’)

 

How would the value c be accessed?

 

Read Kickens reply further, he answers this question.

Value 'c' would be assigned to the first available numeric key, which in this instance is 0.

To access 'c' you would use:

 

$array['a'][0];

Link to comment
Share on other sites

Yes, that is a multi-dimension array.  You would access the value 1 of key b using:

$array['a']['b'];

 

The first level you need to get into is key 'a', then you need to go into key 'b' to get the 1.

 

The value 'c' would be assigned to index 0.  When PHP auto-indexes it uses either 0 if there are no existing numeric indexes, or whatever the highest numerical index is, +1 as the new index

 

Kiken thanks, that is exactly what I was getting at.

 

Thank you all for your input!

 

Link to comment
Share on other sites

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.