I really wonder why @Cârnăciov deleted his answer, it was far better than the one that was accepted.
Dumping the priorities in a global is ok for a test, but if you solve all your problems by littering the global namespace you'll soon be in trouble.
You could avoid this with the use()
keyword like Cârnăciov suggested.
But you can simply define your priorities as a static variable directly inside the function. It will behave exactly like a global, except nobody but the comparison function will see it.
The PHP7 spaceship operator can also be used to simplify the comparison, it's tailor-made for that:
function sort_tags ($a, $b) {
static $prio = array(
"research" => 0,
"strategy" => 1,
"naming" => 2,
"identity" => 3,
"packaging" => 4,
"environment" => 5,
"digital" => 6,
);
return $prio[$a->slug] <=> $prio[$b->slug];
}
Or you can use arrow functions too. These allow you to emulate the convoluted way JavaScript has handled closures these last 25 years :D
(sorry, just a little joke)
$sort_tags = (function () { // this anonymous function closes over "prio"
$prio = array( // and returns the actual comparison function
"research" => 0,
"strategy" => 1,
"naming" => 2,
"identity" => 3,
"packaging" => 4,
"environment" => 5,
"digital" => 6,
);
// the comparison function sees "prio" captured in the upper level closure
return fn ($a, $b) =>
$prio[$a->slug] <=> $prio[$b->slug]; // handles the -1, 0 and 1 cases in one go
})(); // immediately invoked function expression, just like in JavaScript
A silly example demonstrating that you no longer need a global:
file test.php
<?php
function demo () {
$sort_tags = function ($a, $b) {
static $prio = array(
"research" => 0,
"strategy" => 1,
"naming" => 2,
"identity" => 3,
"packaging" => 4,
"environment" => 5,
"digital" => 6,
);
return $prio[$a->slug] <=> $prio[$b->slug];
};
$sample = [
(object)["slug" => "naming" , "id" => 1],
(object)["slug" => "packaging", "id" => 2],
(object)["slug" => "research" , "id" => 3],
(object)["slug" => "strategy" , "id" => 4]];
echo "<br>before usort:<br><br>";
foreach ($sample as $item) echo $item->slug, " => ", $item->id, "<br>";
echo "<br>after usort:<br><br>";
usort ($sample, $sort_tags);
foreach ($sample as $item) echo $item->slug, " => ", $item->id, "<br>";
}
demo();
?>
browser output
before usort:
naming => 1
packaging => 2
research => 3
strategy => 4
after usort:
research => 3
strategy => 4
naming => 1
packaging => 2