php - Why does this loop give up halfway through? -


$ list is a very large array with a series of words, some of which There are 5 letters and some of which are small, this loop will unset all the values ​​where the word length exceeds 5 characters. ($ I = 0; $ i & lt; count ($ list); $ i ++) {$ list [$ i] = trim ($ list [$ i] for ); If (strlen ($ list [$ i])> 5) {unset ($ list [$ i]); Although}, it cuts for almost half the $ list and cuts the dead, there are words that are still more than 5 characters in the list, but only start about halfway through. What is it left? Should I run that loop multiple times?

You are counting every time, and you are unstable before loops before loops It is a good practice to do the number of calls only once, because it greatly reduces the number of function calls you make.

  $ count = count ($ list); ($ I = 0; $ i & lt; $ count; $ i ++) {$ list [$ i] = trim ($ list [$ i]) for; If (strlen ($ list [$ i])> 5) {unset ($ list [$ i]); }}   

Or for fun, use array_map / array_filter:

  $ list = array_map ('trim', $ list); $ List = array_filter ($ list, function ($ s) {return strollon ($ s) & lt; = 5;});    

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -