PROGRAMMING/PHP

[PHP] usort()

OJR 2008. 9. 5. 16:33
http://phpman-en.tistory.com/316


<?php
class TestObj
{
    var
$name
;

    function
TestObj($name
)
    {
       
$this->name = $name
;
    }

   
/* This is the static comparing function: */
   
function cmp_obj($a, $b
)
    {
       
$al = strtolower($a->name
);
       
$bl = strtolower($b->name
);
        if (
$al == $bl
) {
            return
0
;
        }
        return (
$al > $bl) ? +1 : -1
;
    }
}

$a[] = new TestObj("c"
);
$a[] = new TestObj("b"
);
$a[] = new TestObj("d"
);

usort($a, array("TestObj", "cmp_obj"
));

foreach (
$a as $item
) {
    echo
$item->name . "\n"
;
}
?>

'PROGRAMMING > PHP' 카테고리의 다른 글

PHP Reference  (0) 2009.04.02
[PHP]cookie와 session  (0) 2009.04.02
PHP 1.0e-10  (0) 2009.03.10
PHP 성능향상  (0) 2008.12.11
PHP 유용한 링크  (0) 2008.11.24