I would like to have this $sort_flags array available within the compare_by_flags function, but I didn't find a way to this, is it possible?
public function sort_by_rank(array $sort_flags = array()) {
uasort($this->search_result_array, array($this, 'compare_by_flags'));
}
private static function compare_by_flags($a, $b) {
// I want to have this $sort_flags array here to compare according to those flags
}
If you use php < 5.3 then you can just use instance variables:
public function sort_by_rank(array $sort_flags = array()) {
$this->sort_flags = $sort_flags;
uasort($this->search_result_array, array($this, 'compare_by_flags'));
}
private static function compare_by_flags($a, $b) {
// I want to have this $sort_flags array here to compare according to those flags
}
otherwise - use closures:
public function sort_by_rank(array $sort_flags = array()) {
uasort($this->search_result_array, function($a, $b) use ($sort_flags) {
// your comparison
});
}
You don't mention what you want to achieve by passing the $sort_flags
variable, but you might find this answer of mine useful (either as it stands, or as an example if you want to achieve something different).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With