Looping through the contents of an array

Source: Internet
Author: User

In this lesson, you'll learn to use the foreach loop, which is designed to handle arrays and certain types of PHP objects. the foreach loop can be used in two different ways. the first gives you the access to the value of each array element. the syntax looks like this. the first point to note is that foreach is written as one word. it's a key word and if you put a space between for and each it won't work. inside the parenthesis, you have the name of the array you want to loop through followed by the as keyword. and then, a temporary variable to store the value of the current array element each time the loop runs.

So, let's see how that works. i'm going to use a foreach loop to loop through this characters array to build an unordered list. so, I need to add a PHP code block between the UL text. then for each, and the name of the array, which is characters, the as keyword and then a variable to store the value of each array element.

It doesn' t matter what you call the variable. i'm going to use name, and then some curly braces for the code that will be executed in the loop, and so we just echo. i'll use a double quoted string, Li tag, name, closing Li tag. and that's it.

it's quite simple and quite elegant to use a foreach loop with an array like this. let's just save that page and view it in a browser. and there we have a nice unordered list. as the name suggests, a foreach loop accesses each array element and unlike a for loop, it doesn't use a counter. so, if you don't want to loop through the whole array, it's better to use a for loop and break out of the loop when the counter reaches a certain value.

Alternatively, you need to initialize a counter outside of foreach loop and then increment it each time the loop runs. but this is best used if you want to use the whole of the array. the other way of using a foreach loop, gives you access to both the key and value of each array element. using the foreach loop like this is Special useful for associative arrays and the syntax is very similar to an associative of array. after the as keyword, you add two temporary variables separated by the same operator that's used to create name value pairs in the associative array.

The first variable stores the key of the current element and the second one stores its value. so, let's see that in operation. here I 've got an associative array and what I'm going to do is I'm going to put it in the body of this HTML file as a series of paragraphs. so, foreach, of course, we need to have a PHP block first. so, inside the PHP block, foreach.

Then the name of the array is descriptions, the as keyword. and then, I need two variables. i'm going to use key which is very simple and then the operator, which is an equal sign, followed by a greater than sign. and I'll use description rather than descriptions because that's what it is that I'm showing. and then, a pair of curly braces.

And inside there, We're re going to echo and we'll use double quoted string and a p tag and we'll start off with key is, and then description followed by a period. and then closed p. there we are.

Let's just save that, and view it in a browser. so, what's happened here is that the key has been displayed. in this case, it's Earth, followed by the word is, and then by the description. so, we 've got the key is description. and the same has happened with each array element in the associate of array. so, the foreach loop offers a convenient way of looping through every element of an array. in it's basic form, it stores the value of the current array element in a temporary variable. and when used with an associative array, you can define two temporary variables. one for the current elements key, and the other for its value.

Looping through the contents of an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.