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 - PHP Tutorial
From the course: PHP for Web Designers
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Storing multiple values in a variable as an array4m 28s
-
(Locked)
Inspecting an array's elements4m 13s
-
(Locked)
Challenge: Inserting the appropriate alt text1m 27s
-
(Locked)
Solution: Inserting the appropriate alt text5m 26s
-
(Locked)
Displaying an array as a comma-separated list3m 13s
-
(Locked)
Looping through an array's values7m 17s
-
(Locked)
Labeling array elements7m 21s
-
(Locked)
Looping through an array's labels and values5m 29s
-
Finding if a value exists in an array3m 51s
-
(Locked)
Challenge: Displaying a seasonal feature3m 6s
-
(Locked)
Solution: Displaying a seasonal feature8m 30s
-
(Locked)
-
-
-
-