Home Backend Development PHP Tutorial PHP的 foreach each list

PHP的 foreach each list

Apr 26, 2018 am 09:54 AM
foreach list

This article mainly introduces the foreach each list about PHP, which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

<?php
 
/*foreach (函数名 as 地址下标 => 该下标对应的数值)
$arr = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;];
foreach( $arr as $key=>$val) {
var_dump($key.&#39;--------&#39;.$val.&#39;<br  />&#39;);

}
Copy after login



The use of list is similar to directly copying the value in the array to the variable of the list, and then outputting


  $arr = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;];

   list($a,$b,$c,$d,$e) = $arr;

   var_dump($a,$b,$c,$d,$e);
Copy after login



Each is equivalent It is used to read by row, but this is to read the data corresponding to the address one by one

  $arr = [&#39;abc&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;];
  var_dump(each($arr));
    var_dump(each($arr));
  var_dump(each($arr));
    var_dump(each($arr));
  var_dump(each($arr));
Copy after login



This is a combination of list and each, similar to the use of foreach When the output address is the same as the corresponding value

 $arr = [&#39;abc&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;];
list($key,$val) = each($arr);
var_dump($key,$val);
    list($key,$val) = each($arr);
var_dump($key,$val);
list($key,$val) = each($arr);
var_dump($key,$val);
list($key,$val) = each($arr);
var_dump($key,$val);
?>
*/
Copy after login



The above is the detailed content of PHP的 foreach each list. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

​The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

How to implement Redis List operation in php How to implement Redis List operation in php May 26, 2023 am 11:51 AM

List operation //Insert a value from the head of the list. $ret=$redis->lPush('city','guangzhou');//Insert a value from the end of the list. $ret=$redis->rPush('city','guangzhou');//Get the elements in the specified range of the list. 0 represents the first element of the list, -1 represents the last element, and -2 represents the penultimate element. $ret=$redis->l

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

How to convert JSONArray to List in Java How to convert JSONArray to List in Java May 04, 2023 pm 05:25 PM

1: JSONArray to ListJSONArray string to List//Initialize JSONArrayJSONArrayarray=newJSONArray();array.add(0,"a");array.add(1,"b");array.add(2,"c") ;Listlist=JSONObject.parseArray(array.toJSONString(),String.class);System.out.println(list.to

How to sort a list using List.Sort function in C# How to sort a list using List.Sort function in C# Nov 17, 2023 am 10:58 AM

How to sort a list using the List.Sort function in C# In the C# programming language, we often need to sort the list. The Sort function of the List class is a powerful tool designed for this purpose. This article will introduce how to use the List.Sort function in C# to sort a list, and provide specific code examples to help readers better understand and apply this function. The List.Sort function is a member function of the List class, used to sort elements in the list. This function receives

What are the common methods of List in Java basics What are the common methods of List in Java basics May 14, 2023 am 10:16 AM

1. Introduction to List interface List is an ordered collection and a repeatable collection. It inherits the Collection interface. Repeated elements can appear in the List collection, and the element at the specified position can be accessed through the index (subscript). 2. List common methods - voidadd (intindex, Obejctelement) method 1. The voidadd (intindex, Obejctelement) method inserts the element element at the specified position and moves the subsequent element back one element. 2.voidadd(intindex,Obejctelemen

How to convert array to List in Java How to convert array to List in Java Apr 19, 2023 am 09:13 AM

1. The most common way (not necessarily the best) is through Arrays.asList(strArray). After converting the array into List, you cannot add or delete the List, you can only check and modify it, otherwise an exception will be thrown. Key code: Listlist=Arrays.asList(strArray);privatevoidtestArrayCastToListError(){String[]strArray=newString[2];Listlist=Arrays.asList(strArray);//Insert a piece of data into the converted list list.add(" 1&quot

See all articles