I have a PHP array
I want to sort it alphabetically and keep a precise entry at the top:
$arr = array ("Orange", "Banana", "Strawberry", "Apple", "Pear");
asort($arr);
Now this will output:
Apple, Banana, Orange, Pear, Strawberry
I want it to keep Orange as the first entry then reorder the others:
Orange, Apple, Banana, Pear, Strawberry
Thanks.
You can pass in an element to keep at the top using a custom function and
uasort
:It shouldn't matter where Orange is in the array, it'll find its way to the front.
Edit: Note, the
<=>
operator requires PHP 7. You can replace with a call tostrcmp
if you're using 5Get first element from array, then return it back:
Update with unknown
orange
position: