Monday, 9 January 2017

php Sort an Array by keys based on another Array?


<?php
$order = array(1,5,2,4,3,6);

$array = array(
    1 => 'one',
    2 => 'two',
    3 => 'three',
    4 => 'four',
    5 => 'five',
    6 => 'six'
);

uksort($array, function($key1, $key2) use ($order) {
    return (array_search($key1, $order) > array_search($key2, $order));
});
print_r($array);
/*output

Array
(
    [1] => one
    [5] => five
    [2] => two
    [4] => four
    [3] => three
    [6] => six
)
*/ 

?>

0 comments:

Post a Comment