From the course: PHP for Web Designers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Labeling array elements

Labeling array elements - PHP Tutorial

From the course: PHP for Web Designers

Start my 1-month free trial

Labeling array elements

Using numbers to identify array elements is fine in many situations, particularly if the array is a simple collection of related values, like this array of flowers here. But it does have its limitations. Incidentally, this type of array is often called an indexed array. The number acting as a sort of index for each element. But PHP also lets you use text instead of automatically assigned numbers as the key for each array element. In effect, the key acts as a label. So let's go to our editing program and create such an array. The way that you create an array like this is very similar. Let's begin by using a variable. The variable I'm going to use is features, and equals, and then array. The array keyword, are a comma separated list. Inside a pair of parentheses. With this type of array I like to put everything on a separate line. It makes it a lot easier to read. An that makes maintenance a lot easier. So let's indent a little bit. And, we begin, with the array key, which is a string…

Contents