Next Previous Contents

7. Arrays

7.1 Creating Arrays

When you create an array with string literal indices, treat the key-value pairs like a block of code. The first pair should begin in new line and with an indent. The closing bracket should stay in new line after last key-value pair.

Example:

$arr_items = array(
    'key1'    => 'val1',
    'key2'    => 'val2',
);

There is nothing bad in leaving comma after the last pair 'key' => 'val' and there is some good. You take advantage of much faster adding new elements to the definition of existing array or changing order of elements.

7.2 Using Arrays

You should always use quotes around a string literal array index.

Example:

$arr_items['name']

If you want to know why it so important visit PHP manual, array section.


Next Previous Contents