[ create a new paste ] login | about

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

PHP, pasted on Jun 19:
<?php
/**
 * @param array $array
 * @param string|int $by key/offset
 * @param array $order
 * @return array
 */
function array_multisort_by_order(array $array, $by, array $order)
{
    $order = array_flip($order);
    $params[] = $array[$by];
    foreach($params[0] as &$v) $v = $order[$v];
    foreach($array as &$v) $params[] = &$v; unset($v);
    call_user_func_array('array_multisort', $params);
    return $array;
}
$array = array(
            'note' => array('test', 'test1', 'test2'),
            'year' => array('2011','2010', '2012'),
            'type' => array('conference', 'journal', 'conference'),
        );
// Usage:
$array = array_multisort_by_order($array, 'type', array('conference', 'journal'));

print_r($array);
?>


Output:
Array
(
    [note] => Array
        (
            [0] => test
            [1] => test2
            [2] => test1
        )

    [year] => Array
        (
            [0] => 2011
            [1] => 2012
            [2] => 2010
        )

    [type] => Array
        (
            [0] => conference
            [1] => conference
            [2] => journal
        )

)


Create a new paste based on this one


Comments: