Looping through the contents of an array

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 particularly 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 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.

时间: 2024-10-30 01:47:55

Looping through the contents of an array的相关文章

How do you copy the contents of an array to a std::vector in C++ without looping? (From stack over flow)

I have an array of values that is passed to my function from a different part of the program that I need to store for later processing. Since I don't know how many times my function will be called before it is time to process the data, I need a dynam

How do I remove a particular element from an array in JavaScript?

9090down voteaccepted Find the index of the array element you want to remove, then remove that index with splice. The splice() method changes the contents of an array by removing existing elements and/or adding new elements. var array = [2, 5, 9]; co

Awk by Example--转载

原文地址: http://www.funtoo.org/Awk_by_Example,_Part_1?ref=dzone http://www.funtoo.org/Awk_by_Example,_Part_2 http://www.funtoo.org/Awk_by_Example,_Part_3 In defense of awk In this series of articles, I'm going to turn you into a proficient awk coder. I'

常用的X++

Simple select statement examplestatic void selectExample(Args _args){CustTable custTable;int counter;;select * from custTable;while(custTable.AccountNum){print custTable.Name + " "+ custTable.AccountNum;next custTable;counter++;}print "The

Python内置函数——bytearray()-暂疑

英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence

常用的算法思想总结

对于计算机科学而言,算法是一个非常重要的概念.它是程序设计的灵魂,是将实际问题同解决该问题的计算机程序建立起联系的桥梁.接下来,我们来看看一些常用的算法思想. (一)穷举法思想 穷举法,又称为强力法.它是一种最为直接,实现最为简单,同时又最为耗时的一种解决实际问题的算法思想. 基本思想:在可能的解空间中穷举出每一种可能的解,并对每一个可能解进行判断,从中得到问题的答案. 使用穷举法思想解决实际问题,最关键的步骤是划定问题的解空间,并在该解空间中一一枚举每一个可能的解.这里有两点需要注意,一是解空

python学习之函数

1.函数名可以被赋值 比如: def aaa(): pass b = aaa//将函数名字赋值给b b()//跟aaa()效果一样 2.return 2.1.如果函数不写return的话,会默认返回None 2.2.return后,函数下面的语句不会被执行,中断函数操作 2.3.return个什么东西都行,哪怕是个列表..... 3.pycharm使用断点调试的话,需要用debug模式(向右小箭头的小虫子) 4.参数: 默认参数必须写在后边 def aaa(a1, a2 = 1): pass//

python3.4 build in functions from 官方文档 翻译中

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct

swift中文文档- 类型转换

未翻译完 待续(英语烂,求斧正) Type Casting 类型转换 Type casting is a way to check the type of an instance, and/or to treat that instance as if it is a different superclass or subclass from somewhere else in its own class hierarchy. 类型转换是检测实例所属类型的一种方法,和/或 去对待实例好像它是一个