2011-04-13 136 views
1

如何通过权重和类型对关联数组排序?PHP:排序数组帮助

输入

array(
    'a'  => array('type' => 't1', 'weight' => 1, 'text' => 'text1'), 
    'b'  => array('type' => 't1', 'weight' => 3, 'text' => 'text2'), 
    'c'  => array('type' => 't2', 'weight' => 5, 'text' => 'text3'), 
    'd'  => array('type' => 't1', 'weight' => 2, 'text' => 'text4'), 
    'e'  => array('type' => 't2', 'weight' => 4, 'text' => 'text5'), 
    'f'  => array('type' => 't2', 'weight' => 4, 'text' => 'text6') 
); 

所需的输出

array(
    'a'  => array('type' => 't1', 'weight' => 1, 'text' => 'text1'), 
    'd'  => array('type' => 't1', 'weight' => 2, 'text' => 'text4'), 
    'b'  => array('type' => 't1', 'weight' => 3, 'text' => 'text2'), 
    'e'  => array('type' => 't2', 'weight' => 1, 'text' => 'text5'), 
    'f'  => array('type' => 't2', 'weight' => 1, 'text' => 'text6'), 
    'c'  => array('type' => 't2', 'weight' => 5, 'text' => 'text3') 
); 

类型 “T2” 在启动时必须出现在阵列的端部,所有其他类型。

重量必须在键入后排序。

我正在使用uasort与自定义比较功能,但我挣扎。这里是我有什么,但它不工作:

function my_comparer($a, $b) { 
    return ($a['type'] !== 't2' && $b['type'] === 't2') 
     ? -1 
     : $a['weight'] - $b['weight']; 
} 

回答

2

你的函数不考虑($ a ['type'] =='t2')

function my_comparer($a, $b) { 
    if (($a['type']==='t2') && ($b['type']!=='t2')) return -1; 
    if (($b['type']==='t2') && ($a['type']!=='t2')) return 1; 
    return ($a['weight'] - $b['weight']); 
} 
+0

谢谢!我忘了我不得不面对相反的情况。 – 2011-04-13 16:19:35

0

助手功能

function arrayColumnSort(&$array, $directions) { 
    // collect columns 
    $columnNames = array_keys($directions); 
    $columns = array(); 
    foreach ($array as $row) { 
     foreach ($columnNames as $columnName) { 
      if (!isset($columns[$columnName])) { 
       $columns[$columnName] = array(); 
      } 

      $columns[$columnName][] = isset($row[$columnName]) ? $row[$columnName] : null; 
     } 
    } 

    // build array_multisort params 
    $params = array(); 
    foreach ($directions as $columnName => $direction) { 
     $params = array_merge(
      $params, 
      array($columns[$columnName]), 
      is_array($direction) ? $direction : array($direction) 
     ); 
    } 

    $params[] =& $array; 

    // sort 
    call_user_func_array('array_multisort', $params); 
} 

呼叫

arrayColumnSort($data, array(
    'type' => SORT_ASC, 
    'weight' => SORT_ASC, 
)); 
1

试试这个:

function my_comparer($a, $b) { 
    if($a['type'] == $b['type']){ 
     return $a['weight'] - $b['weight']; 
    }else{ 
     if($a['type'] > $b['type']) return 1; 
     else return -1; 
    } 
} 

(警告,这是未经测试)

0

更简单的办法是array_multisort

$data = array(
    'a'  => array('type' => 't1', 'weight' => 1, 'text' => 'text1'), 
    'b'  => array('type' => 't1', 'weight' => 3, 'text' => 'text2'), 
    'c'  => array('type' => 't2', 'weight' => 5, 'text' => 'text3'), 
    'd'  => array('type' => 't1', 'weight' => 2, 'text' => 'text4'), 
    'e'  => array('type' => 't2', 'weight' => 4, 'text' => 'text5'), 
    'f'  => array('type' => 't2', 'weight' => 4, 'text' => 'text6') 
); 

// Obtain a list of columns 
foreach ($data as $key => $row) { 
    $type[$key] = $row['type']; 
    $weight[$key] = $row['weight']; 
} 
array_multisort($type, SORT_ASC, $weight, SORT_ASC, $data); 

另外,如果你添加任何数量的类型的这个作品你要以相同的方式排序。