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