How does PHPforeach determine as the last most efficient array?

Source: Internet
Author: User
The method used now seems stupid. what is the simple method? The method used now is
It seems very stupid to do this. what is simple writing? Reply: I have another idea: since what you want is to execute something else at the last time, that is, to do something special.
In my opinion, the last element should not be put in the original array list, but should be extracted separately, so the following method is available:
$ Last = array_pop ($ list );
Foreach ($ list as $ v) do_common ($ v );
// Special action
Do_special ($ last); you can use end ($ array) directly to obtain the last value. you can use the php pointer function.


  "; Echo end ($ array2); echo"
"; Echo end ($ array3 );
$ List = array ('A', 'B', 'C ');
Foreach ($ list as $ k => $ v ){
If ($ v! = End ($ list )){
// Not the last item
} Else {
// Last item
}
} Mentioning count ($ list) outside the loop can improve efficiency. Are you sure you can implement this?
$ I starts from 0, and count ($ list) is 1 larger than $ I. Do you think "last" is output? The initial value of $ I is equal to 1.
Some suggestions for your code should be made to improve efficiency.
Assign count ($ list) to another value, such as $ len, and put this value out of the loop. The judgment condition becomes if ($ I! = $ Len ).
You can judge as follows:
$ Len = count ($ list );
$ LastVal = end ($ list );
If ($ lastVal ==$ list [$ len-1]) {
Echo 'Last file ';
}
?> $ Count = count ($ result)-1;
Foreach ($ result as $ key => $ v ){
If (empty ($ v) continue;
$ SQL. = "('";
$ SQL. = implode ("','", $ v );
$ SQL. = "')";
If ($ key <$ count ){
$ SQL. = ",";
}
} 1 determine $ val = end ($ array) if the array values are not repeated)
On the second floor, I want to change my mind.
3. the code is concise.
$ Fruit = array ('a' => 'apple', 'B' => 'bana', 'C' => 'Cranberry ');
Reset ($ fruit );
While (list ($ key, $ val) = each ($ fruit )){
Echo "$ key => $ val \ n ";
If (! Current ($ fruit) echo 'Last ';
}

High efficiency is really a problem

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.