Home > Backend Development > PHP Tutorial > list-each-while traverse array

list-each-while traverse array

WBOY
Release: 2016-07-29 09:15:21
Original
907 people have browsed it
$name = array(
		'孟子','孔子','孙子','老子'
	);
while($ele = each($name)){
		$key = $ele['key']; // == $ele[0]
		$value = $ele['value']; // $ele[2]
		var_dump($key,$value);
		echo "<br>";
	}
Copy after login

Although this is not commonly used, it is very useful for understanding the concept of array pointers (the best is to use foreach)

It can also be upgraded, using the List structure

The List structure uses an indexarray to initialize multiple arrays at the same time variables

$arr = array(0=>"some",1=>"many",2=>"much");
	list($v1,$v2,$v3) = $arr;
	var_dump($v1,$v2,$v3);
Copy after login

so it is optimized like this

	while(list($key,$value) = each($name)){
		// $key = $ele['key']; // == $ele[0]
		// $value = $ele['value']; // $ele[2]
		var_dump($key,$value);
		echo "<br>";
	}
Copy after login

The above introduces list-each-while traversing arrays, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template