Saturday, October 13, 2012

sort array 2 dimensi

contoh :

$arr = array();
$arr[0]['id'] = 1;
$arr[0]['name'] = 'sathish';
$arr[0]['role'] = 1000;
$arr[1]['id'] = 2;
$arr[1]['name'] = 'siyam';
$arr[1]['role'] = 200;
$arr[2]['id'] = 3;
$arr[2]['name'] = 'prabhu';
$arr[2]['role'] = 1234;
$arr[3]['id'] = 4;
$arr[3]['name'] = 'hawai';
$arr[3]['role'] = 111;
$arr[4]['id'] = 5;
$arr[4]['name'] = 'dean';
$arr[4]['role'] = 999;
$result = sortTwoDimensionArrayByKey($arr,'name');

echo "
";
print_r($result);
echo "
";

function sortTwoDimensionArrayByKey($arr, $arrKey, $sortOrder=SORT_ASC)
{
    foreach($arr as $key => $row)
    {
        $key_arr[$key] = $row[$arrKey];
    }
    array_multisort($key_arr, $sortOrder, $arr);
    return $arr;
}
?>

0 comments: