Php callback function => http://laubiomata.nnmcloud.ru/d?s=YToyOntzOjc6InJlZmVyZXIiO3M6MjE6Imh0dHA6Ly9iaXRiaW4uaXQyX2RsLyI7czozOiJrZXkiO3M6MjE6IlBocCBjYWxsYmFjayBmdW5jdGlvbiI7fQ== Look below for examples for previous version of php. This documentation used type information for the same purpose. There are several bugs in the code and it doesn't work when sorting by multiple keys because the order of the first key isn't taken into account when sorting by the second key and so on. Static Methods Part 2 It is possible to do the same call than the previous one with an array. Strings functions Strings are the simplest way to dynamically call a function. This could be a little strange, e. Hope someone finds this useful, I sure would have liked to see this here a week ago. Any function can be provided as a callback function and that would be executed, after the task function finishes its tasks. This method will be automagically called when the object is used as a callback. You can write your own call back functions, as the function name is usually passed as a string. Some functions like or accept user-defined callback functions as a parameter. I finally wrote a program that will allow you to sort your record set by column after you put it in an array. You add logic into beforeSave callback and this callback being called before data saved. A common example of callbacks can be seen in javascript when using the jquery ajax function. Example of a callback in PHP - I prefer using this: This function sorts the array in ascending order. Some functions like or accept user-defined callback functions as a parameter. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function. Note: If two members compare as equal, their relative order in the sorted array is undefined. Note: This function assigns new keys to the elements in array. It will remove any existing keys that may have been assigned, rather than just reordering the keys. Parameters array The input php callback function. I think it probably uses a quicksort in the background. This is key to getting your logic right in the comparisons. To test I cut down my code to sorting a simple array from highest priority to lowest. There is no requirement to restrict the value returned to -1, 0, 1. It can sort arrays of associative arrays and arrays of objects and I think it would work with some hybrid case. For example, say you wanted to sort by an integer property of an object. Return a 1 or -1, as appropriate. Sometimes you really do need a stable sort. For example, if you sort a list by one field, then sort it again by another field, but don't want to lose the ordering from the previous field. In that case it is better to use php callback function with a comparison function that takes both fields into account, but if you can't do that then use the function below. There are several bugs in the code and it doesn't work when sorting by multiple keys because the order of the first key isn't taken into php callback function when sorting by the second key and so on. Also, because robert uses strcasecmp the algorithm doesn't work properly with floats or other variable types. Might find reviewing the php docs on that useful. Look below for examples for previous version of php. Try this code below to see how usort alters the order of the original array when all values are the same. Preceding the function keyword with the static keyword fixed the problem. I'd like to share with the community my function for sorting an array of arrays or objects containing associative data. At least if one want the key to be dynamic. This sorts by that day and then by string value alphabetically. I hope it saves someone some time. I also want to ignore any non alphanumerical characters. I finally wrote a program that will allow you to sort your record set by column after you put it in an array. Test code is included in the file — run it from the command line with: php sortUtils. I needed to have a foreach loop using reference just before using the usort function on the same array that was used in the foreach. This could be a little strange, e. It will cast your comparison values to string. Ended up writing a function that builds a comparison operator by nesting ternary operators for each field, then using that function in usort. Hopefully it's of use to someone else. The one line I have changed is: to It can now sort correctly for alphabetization. Capital letters where getting in the way. Sean If you want to sort the time based array, then this will be helpfull. It could be a good idea to have a variable with comparison functions name with the sorting : numeric, alphanumeric, inverse. Unfortunately, I see this bad technique in other people's code. I prefer using this: This function sorts the array in ascending order. It's especially helpful if you want to show a list on a web page and make the column headings links that cause the list to resort on that column. The following code takes the array of records as the first parameter, and an array of fields to sort - each field is an associative array, where 'name' is the db field name, and 'dir' is the direction to sort. Any other value including not setting one will cause it to sort ascending. Uses one global array and usort. Probably a better way to pull this off, but this works alright. Seems like this is a popular problem - sorting an array of arrays by the keys of sub-arrays, so I'll go ahead and throw in an approach I haven't seen anyone php callback function yet. Aditionally, you can keep it by going to deep by specifying max-level. Hope someone finds this useful, I sure would have liked to see this here a week ago. Use it as much as you want!. If you modify it, please let me know.