Concept

How can PHP arrays be sorted?

ComputerScienceOne / Searching

"PHP’s sort() function can be used to sort elements in ascending order. This is useful if you have arrays of numbers or strings, but doesn’t work very well if you have an array of mixed types or objects. 1 $arr = array(\"banana\", \"Apple\", \"zebra\", \"apple\", \"bananas\"); 2 sort($arr); 3 //arr is now (\"Apple\", \"apple\", \"banana\", \"bananas\", \"zebra\") 4 5 $arr = array(10, 8, 3, 12, 4, 42, 7, 108); 6 sort($arr); 7 //arr is now (3, 4, 7, 8, 10, 12, 42, 108) PHP provides a more versatile sorting function, usort() ( u ser defined sort ) that accepts a comparator function that it uses to define the ordering of elements. To pass a comparator function to the usort() function, we pass a string value containing the"

Related Ideas

How can PHP arrays be sorted? | ComputerScienceOne | Bifalgorithm | Bifalgorithm