[ create a new paste ] login | about

Link: http://codepad.org/ItyIa7fB    [ raw code | output | fork ]

PHP, pasted on Jul 2:
<?php

$myarray = array(
    array(
        "id"=>10,
        "hits"=>80
    ),
    array(
        "id"=>14,
        "hits"=>50
    ),
    array(
        "id"=>15,
        "hits"=>700
    ),
    array(
        "id"=>18,
        "hits"=>200
    )
);

function customsort($a,$b){
    if($a["id"]==18){ //or b==18?
        return -1;
    } else {
        return $a["hits"]>$b["hits"]?1:-1;
    }
}

usort($myarray,"customsort");

print_r($myarray);

?>


Output:
Array
(
    [0] => Array
        (
            [id] => 18
            [hits] => 200
        )

    [1] => Array
        (
            [id] => 14
            [hits] => 50
        )

    [2] => Array
        (
            [id] => 10
            [hits] => 80
        )

    [3] => Array
        (
            [id] => 15
            [hits] => 700
        )

)


Create a new paste based on this one


Comments: