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