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.

Looping through an array's labels and values

Looping through an array's labels and values - PHP Tutorial

From the course: PHP for Web Designers

Start my 1-month free trial

Looping through an array's labels and values

In an associative array, the elements key acts as a sort of label. And when looping through an associative array, you have the option to work only with the array values or with both the keys, in other words the labels, and the values independently. If you're interested only in the value of each array element, you can use a for each loop in exactly the same way as earlier in this chapter with an indexed array. That's one that uses numbered keys. Let's refresh our memory of how that's done. Let's imagine that all we want to do is to display the content of each tag line in this features array. So we just scroll down a bit further into the page and under this h2 heading here, we'll add a PHP block. And inside the block we want a foreach loop, foreach, remember, is written as single word. Don't split them up. And the condition goes in parentheses. It's the features array. And then we use the AS keyword to assign the value of each element to a temporary variable. Doesn't matter what you…

Contents