PHP 8.5.0 Beta 1 available for testing

Voting

: one minus one?
(Example: nine)

The Note You're Voting On

salchicha at cable dot net dot co
23 years ago
Here's one I whipped up to allow you to sort an array of a specific class by a member or function:

<?php
// Sort a class by one of its members (even lowercase!!!)
function casort($arr, $var) {
$tarr = array();
$rarr = array();
for(
$i = 0; $i < count($arr); $i++) {
$element = $arr[$i];
$tarr[] = strtolower($element->{$var});
}

reset($tarr);
asort($tarr);
$karr = array_keys($tarr);
for(
$i = 0; $i < count($tarr); $i++) {
$rarr[] = $arr[intval($karr[$i])];
}

return
$rarr;
}
?>

It works very well. For example, I have a Room class with members title, isActive(), date, etc. I can sort an array by casort($rooms, "title") or casort($rooms, "isActive()") and it'll work.

<< Back to user notes page

To Top