php search array for string, then if found, move that string to first item of array -


So assume that I have an array:

  $ people [0] = "Bob"; $ People [1] = "Sally"; $ People [2] = "Charlie"; $ People [3] = "Claire"; If (in_are ("Charlie", $ people)) {// take Charlie into the first item in the array}   

What would be the most effective way to get Charlie first item in the array?

You can use elements to be included in an array.

  $ Pos = array_search ("Charlie", $ people); If ($ pos! == FALSE) {$ item = $ people [$ pos]; Not set ($ people [$ position]); Array_unshift ($ people, $ item); }    

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -