开发者

Is this line of code indicating an array inside another array in php?

$info['schedule'] = array('swimming', 'soccer', 'read book');

J开发者_StackOverflowust going through a tutorial and came across this line of code. Is this code creating an array with 3 variables inside the array $info under the variable 'schedule'?


Yup!!

check it yourself. I would say, key schedule of array $info has an array of strings.

$info['schedule'] = array('swimming', 'soccer', 'read book');


print_r($info);


Yes it is, however it's not under the variable 'schedule' it's called a key. And the key is 'schedule'


Yes, a two dimensional asymmetric array. Use print_r or var_dump functions on an array to see it all.


'schedule' isn't a variable, it's the key in the array info. And yes, the line is creating a new array at that key so $info looks something like this:

['schedule' => ['swimming', 'soccer', 'read book']]


Yes, and you can create another array inside that array. You can have an array that holds an infinite number of arrays which each hold an infinite number of arrays and so on and so forth for infinity. Well, not really, never have enough time or memory to actually instantiate all of them. You can also put objects inside arrays inside objects inside variables inside objects inside arrays.

Anyway, it's a multidimensional array, you'll find quite a lot of use for them. In that example, $info['schedule'][0] would be 'swimming' and $info['schedule'][2] would be 'read book'.

You can do something like:

$info['schedule'] = array(
                         'play' => array('soccer','basketball','baseball','hockey','chess'),
                         'read' => array('A Tale of Two Cities','War and Peace','Moby Dick'),
                         'work' => array('Reports' => array(
                                                                'TPS Reports','Tax Forms'
                                                            ), 
                                             'Busy Work', 'Go to Appointment'
                                            )
                         );

In which $info['schedule']['work']['Reports'][1] would be 'Tax Forms' and $info['schedule']['work'][0] would be 'Busy Work'. Unkeyed array values when setting the array get the numerical keys in order as if there were no named keys. So, count($info['schedule']['work']) would return 3 but only ['work'][0] and ['work'][1] would be valid numeric keys because the third one is ['work']['Reports']

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜