Differences between phpforeach and for statements. Foreach $ tararray (1 East, 2 West, 3 South, 4 North, 5 Southeast, 6 Southwest, 7 Northeast, 8 northwest, 9 North South, 10 East ,); copy the code as follows // foreach
$ Tar = array (
1 => 'East ',
2 => 'West ',
3 => 'south ',
4 => 'North ',
5 => 'Southeast ',
6 => 'southwest ',
7 => 'Northeast region ',
8 => 'Northwest ',
9 => 'North and South ',
10 => 'thes ',
);
The code is as follows:
$ TM = 'West ';
Foreach ($ tar as $ v => $ vv)
{
If ($ vv = $ TM)
{
Echo $ vv. '-'. $ v .'
';
Break;
}
// Echo $ vv;
}
// West-2
//
The code is as follows:
Echo'
';
For ($ I = 1; $ I <= count ($ tar); $ I ++)
{
If ($ tar [$ I] ==$ TM)
{
Echo $ tar [$ I]. '-'. $ I .'
';
Break;
}
}
// West-2
Conclusion: foreach and for results are exactly the same, but in terms of efficiency, foreach is better than for. The homepage for needs to know the array length and then use $ I ++ for operations, page foreach does not need to know the length of the array to automatically detect and enter the key, and value.
Http://www.bkjia.com/PHPjc/371795.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371795.htmlTechArticle//foreach $ tar = array (1 = 'East', 2 = 'West', 3 = 'south', 4 = 'North', 5 = 'south ', 6 = 'Southwest China ', 7 = 'Northeast China', 8 = 'Northwest China ', 9 = 'North and South', 10 = 'East thing',); the code is as follows...