How to sort an array using a custom sort function which requires additional data, in addition to the two objects being compared?
i.e. a structure something like this:
myfunction($item1, $item2, $data)
{
$data->dataDependentSort($item1, $item2);
}
usort($array, "myfunction", $data);
Is this possible without global variables, or reimplementing usort()?
Usort will let you use a class function, but only a static function!
class MySorter
{
function __construct($data)
{
$this->m_data = $data;
}
public function sortItems($item1, $item2)
{
...
}
}
$sorter = new MySorter($data);
usort($myarray, array("sorter", "sortItems")); // doesn't work because sorter isn't a class
usort($myarray, array("MySorter", "sortItems")); // doesn't work because sortItems can't access m_data