Comparison between LED lamps and energy-saving lamps Comparison of several single-dimensional array traversal methods in PHP

WBOY
Release: 2016-07-29 08:46:50
Original
1058 people have browsed it

Copy codeThe code is as follows:


//a
$arr=array('a'=>'abc','b'=>123,'c'=> ;true);
//b
//$arr=range('a','d');
//1
for($i=0;$i echo $arr[$i].', ';
echo '
';
//2
foreach($arr as $key)
echo "$key, ";
echo '
';
//3
foreach($arr as $key=>$val)
echo "$key-$val, ";
echo '
';
//4
reset($arr);
while($item=each($arr)){
echo $item['key'].'-'.$item['value'].', ';
}
echo '
';
//5
reset($arr);
while(list($key,$val)=each($arr)){
echo "$key-$val, ";
}
echo '
';
?>


Use the statement a $arr=array('a'=>'abc','b'=>123,'c'=> ;true); Initialize $arr to get the numerical index array, the output is as follows:
, , ,
abc, 123, 1,
a-abc, b-123, c-1,
a-abc, b-123, c-1,
a-abc, b-123, c-1, use statement b $arr=range('a','d'); to initialize $arr to get the associative array, the output is as follows:
a, b , c, d,
a, b, c, d,
0-a, 1-b, 2-c, 3-d,
0-a, 1-b, 2-c, 3-d,
0 -a, 1-b, 2-c, 3-d, the for loop only has limited numerical indexes; for and foreach do not need to reset() the data after the traversal is completed to make it available for the next traversal, while the each method does .

The above introduces the comparison between LED lamps and energy-saving lamps and the comparison of several single-dimensional array traversal methods in PHP, including the comparison between LED lamps and energy-saving lamps. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!