How does PHP foreach determine the last most efficient for an array?

Source: Internet
Author: User
Tags php foreach
The way it's used now is
It seems foolish to do so. What is the simple wording?

Reply content:

I have another idea: since what you want is to do something else at the end of the day, that is to do something special.
Then I think that the last element should not be placed in the original array list, but should be extracted separately, so the following method:
$last = Array_pop ($list);
foreach ($list as $v) Do_common ($v);
Special action
Do_special ($last); use End ($array) directly to take the last value, you can use the PHP pointer function

   /*** Created by Phpstorm.* User:huage.org* date:2014-09-06* time:13:43 */$array 1=Array(1,2,3,4);$array 2=Array("Lin Chi-ling","Liu Tao","Sui and Tang","Athena Chu");$array 3=Array("Liu Tao",2,"Athena Chu",1,"Lin Chi-ling");Echo End($array 1);//To point the inner pointer of the array to the last cell, for all arraysEcho "
";Echo End($array 2);Echo "
";Echo End($array 3);
$list = Array (' A ', ' B ', ' C ');
foreach ($list as $k = = $v) {
if ($v! = End ($list)) {
Not the last one.
} else {
The last one
}
Putting count ($list) out of the loop can improve efficiency. Are you sure you can do this?
$i start from 0, Count ($list) is 1 larger than $i, do you think it will output "last"? $i initial value equals 1.
A bit of advice on your code should improve efficiency.
Assign count ($list) to another value, such as: $len, put this outside the loop. The condition is judged as if ($i! = $len).
You can judge by this:
$len = count ($list);
$lastVal = End ($list);
if ($lastVal = = $list [$len-1]) {
Echo ' last ';
}
?> $count = count ($result)-1;
foreach ($result as $key = = $v) {
if (empty ($v)) continue;
$sql. = "('";
$sql. = Implode ("', '", $v);
$sql. = "')";
if ($key < $ $count) {
$sql. = ",";
}
}1 array values are not repeated $val ==end ($array)
2 1 Floor said to change the idea of processing
3 sense code is relatively concise
$fruit = Array (' a ' = = ' Apple ', ' b ' = ' banana ', ' c ' = ' cranberry ');
Reset ($fruit);
while (list ($key, $val) = each ($fruit)) {
echo "$key = $val \ n";
if (!current ($fruit)) echo ' last ';
}

It's a question of high efficiency.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.