c:foreach 循环_PHP 7.x — P32:Foreach循环

c:foreach 循环

You can use the for loop to iterate through an array, but to really shine, you need to use the foreach loop. The foreach loop has the following syntax.

您可以使用for循环遍历数组,但是要真正发挥作用,您需要使用foreach循环。 foreach循环具有以下语法。

foreach ($array as $key => $value)

You’ll read the structure like this:

您将阅读如下结构:

  • For each array as key/value … or

    对于每个数组作为键/值…或
  • For each key/value pair inside of an array.

    对于数组内部的每个键/值对。

It means that the foreach loop will loop through each array element and you will have access to each key/value pair of each element. This is great if you’re dealing with associative arrays. If the key is a string, you will not be able to access the element with a numerical index.

这意味着foreach循环将遍历每个数组元素,并且您将有权访问每个元素的每个键/值对。 如果您要处理关联数组,那就太好了。 如果键是字符串,则将无法使用数字索引访问元素。

Sometimes you also need to display the key. For example, let’s say that you’re storing a to-do list inside of an associative array, where each key is the day of the week. You may need to display the key to the user as well. If you don’t, the structure of the foreach statement can be reduced.

有时您还需要显示密钥。 例如,假设您将待办事项列表存储在关联数组内,其中每个键都是星期几。 您可能还需要向用户显示密钥。 如果您不这样做,则可以减少foreach语句的结构。

foreach ($array as $value)

The above structure will only provide you access to the element’s value. You still have the advantage of looping through an array that has either non-numerical keys, or numerical but non-consecutive keys.

上面的结构仅使您可以访问元素的值。 您仍然具有遍历具有非数字键或数字但非连续键的数组的优势。

Let’s look at an example of the foreach loop iterating through a normal array that has numerical indices.

我们来看一个foreach循环遍历具有数字索引的普通数组的示例。

We could use the for loop to loop through the array, or we could go with the foreach loop.

我们可以使用for循环遍历数组,也可以使用foreach循环。

As you can see from the example above, with the foreach loop, the syntax is a lot simpler. We don’t have to use the count() function to get the number of elements inside of the array. We also don’t have to use a counter variable ($i) to keep track of which index we’re on. PHP does all of that for us when we use the foreach loop.

从上面的示例中可以看到,对于foreach循环,其语法要简单得多。 我们不必使用count()函数来获取数组中元素的数量。 我们也不必使用计数器变量( $ i )来跟踪我们在哪个索引上。 当我们使用foreach循环时,PHP会为我们完成所有这些工作。

Examining the example above, PHP:

检查上面的示例,PHP:

  1. Encounters the foreach statement. It knows that it will be iterating through an array.

    遇到foreach语句。 它知道它将遍历一个数组。

  2. The syntax inside of the foreach loop says that each array element value will be accessed.

    foreach循环内部的语法表明,将访问每个数组元素值。

  3. During the first iteration, PHP has access to the first element. When the echo statement is executed, the $value variable points to the first element inside of the array. So, PHP displays “meat shredder claws.”

    在第一次迭代中,PHP可以访问第一个元素。 执行echo语句时, $ value变量指向数组内部的第一个元素。 因此,PHP显示“碎肉爪”。

  4. PHP reaches the end of the loop body and repeats the process until there are no more elements in the array.

    PHP到达循环体的末尾并重复该过程,直到数组中没有更多元素为止。

With the foreach statement, you don’t have to worry about whether the index value that you selected is going to be out-of-bounds. PHP will keep track of that for you.

使用foreach语句,您不必担心所选的索引值是否会超出范围。 PHP将为您跟踪该情况。

The $key and $value elements inside of the foreach loop structure do not have to be named key/value. You can name them whatever you like. For example, let’s say that we have an array of cars. Instead of naming the element $value, we could name it $car and be explicit as to what that value represents.

$键foreach循环结构的内部$价值元素没有被命名的键/值。 您可以随意命名。 例如,假设我们有一系列汽车。 除了命名元素$ value之外 ,我们可以将其命名为$ car,并明确表示该值代表什么。

That’s a lot easier to read than if we used a for loop or if we simply said $value instead of $car.

这比使用for循环或仅说$ value而不是$ car更容易阅读。

Let’s implement the to-do list example that was mentioned earlier. This time, we’re going to need access to both the key and the value. We can name the key/value pair as $key/$value or we can be explicit with our naming. The key stores the day of the week, and the value stores the to-do item. We’ll name the key $day_in_week and we’ll name the value $to_do_item.

让我们实现前面提到的待办事项清单示例。 这次,我们将需要访问键和值。 我们可以将键/值对命名为$ key / $ value ,也可以通过命名来明确。 键存储星期几,值存储待办事项。 我们将键命名为$ day_in_week ,并将值命名为$ to_do_item

When we loop through the $to_do_list array, we have access to both the key and the value. PHP will display the content of the key, followed by a colon, followed by the content of the value for each element in the array. Remember that the foreach structure requires that you use the as keyword, and the arrow (=>) inside of the foreach statement if you’re accessing the key/value pairs.

当我们遍历$ to_do_list数组时,我们可以访问键和值。 PHP将显示键的内容,后跟冒号,然后显示数组中每个元素的值的内容。 请记住,如果要访问键/值对,则foreach结构要求您使用as关键字,并在foreach语句中使用箭头(=>)。

翻译自: https://medium.com/dev-genius/php-7-x-p32-foreach-loop-f38b88249e76

c:foreach 循环

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值