Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use foreach traversal in php

Shulou Source: shulou.com Published: 2022-06-01 03:49:09 09月22日 Update

This article mainly shows you "how to use foreach traversal in php". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to use foreach traversal in php".

Foreach traversal

/ / write foreach ($arr as $k = > $v) {/ /} / / write two foreach ($arr as $v) {/ /} / / write three foreach ($arr as & $v) {/ /} / / write four $arr = [1, 2, 2, 3], [4, 5, 5, 6]; foreach ($arr as list ($1, 2, 6)) {/ /}

1. First of all, you need to know that $v is not addressed in method one and method two, and $v is a copy of the key value. So don't change the array / object itself in foreach, it's useless.

two。 When you don't need a keyname or index, there's nothing to say about writing $k.

3. With regard to the writing method 3 address reference assignment, the advantage is to save space when the value is very large. It is important to note, however, that either way, $v will be retained after the foreach loop, so if you are addressing it, it would be a bad thing to use $v again. The manual recommends unset ($v).

4. The fourth method is for the application of two-dimensional array / object, using list to unpack the key-value array. Note that the number of variables received in list is greater than the length in the actual two-dimensional array will report an error, less than can just not be received, so consider it when you use it.

The above is all the content of the article "how to use foreach traversal in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Writing array content time article object two-dimensional learning help easy to say big thing again variable just bad thing good practical suggestion manual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Huawei Shulou Technology Microsoft