Naps Posted March 15, 2012 Share Posted March 15, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258986-multidimensional-array/ Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258986-multidimensional-array/#findComment-1327647 Share on other sites More sharing options...
Naps Posted March 15, 2012 Author Share Posted March 15, 2012 *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' Quote Link to comment https://forums.phpfreaks.com/topic/258986-multidimensional-array/#findComment-1327648 Share on other sites More sharing options...
kicken Posted March 15, 2012 Share Posted March 15, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258986-multidimensional-array/#findComment-1327649 Share on other sites More sharing options...
scootstah Posted March 15, 2012 Share Posted March 15, 2012 Also would the next line ('c') be automatically assigned to the [edit] alphabetical key of c? That does nothing to clarify your question. The only way to assign anything to the key "c" is to explicitly assign something to the key "c". $array['a]['c'] = 'something'; Quote Link to comment https://forums.phpfreaks.com/topic/258986-multidimensional-array/#findComment-1327650 Share on other sites More sharing options...
Naps Posted March 15, 2012 Author Share Posted March 15, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/258986-multidimensional-array/#findComment-1327651 Share on other sites More sharing options...
AyKay47 Posted March 15, 2012 Share Posted March 15, 2012 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]; Quote Link to comment https://forums.phpfreaks.com/topic/258986-multidimensional-array/#findComment-1327653 Share on other sites More sharing options...
Naps Posted March 15, 2012 Author Share Posted March 15, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/258986-multidimensional-array/#findComment-1327654 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.