排序多维数组

时间:2012-06-25 08:39:40

标签: php arrays

这是一种情况:

蒂姆,罗布和吉姆活着过来。吉姆有一把吉他,卖给了蒂姆。吉姆用这笔钱买了一双新款时尚鞋。 Rob意外地打破了Tim的吉他(他从蒂姆那里买来的),在此之后,Rob为Tim添了一把玩具吉他,Tim没有时间休息。

这是在房间里完成的一系列活动,最先是最新活动。

$all_users_activities = array( 
    case1 => array(username =>'Tim', activity => 'broken', object_type => 'toy', objectname=> 'Guitar'),
    case2 => array(username =>'Rob', activity => 'gifted', object_type => 'toy', objectname=> 'Guitar'),
    case3 => array(username =>'Rob', activity => 'broken', object_type => 'music', objectname=> 'Guitar'),
    case4 => array(username =>'Tim', activity => 'bought', object_type => 'music', objectname=> 'Guitar'),
    case5 => array(username =>'Jim', activity => 'bought', object_type => 'fashion', objectname=> 'shoes'),
    case6 => array(username =>'Jim', activity => 'sold', object_type => 'music', objectname=> 'Guitar')
    );

现在,我想根据以下内容对此数据进行排序:

  1. 用户名
  2. objectname('object_type'相同:'toy''guitar'与'music''guitar'不同)
  3. 活性

1 个答案:

答案 0 :(得分:0)

检查出来

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
    echo "<pre>";
    print_r($array);
}
$all_users_activities=array( 
    case1 => array('username' =>'Tim', activity => 'broken', object_type => 'toy', objectname=> 'Guitar'),
    case2 => array('username' =>'Rob', activity => 'gifted', object_type => 'toy', objectname=> 'Guitar'),
    case3 => array('username' =>'Rob', activity => 'broken', object_type => 'music', objectname=> 'Guitar'),
    case4 => array('username' =>'Tim', activity => 'bought', object_type => 'music', objectname=> 'Guitar'),
    case5 => array('username' =>'Jim', activity => 'bought', object_type => 'fashion', objectname=> 'shoes'),
    case6 => array('username' =>'Jim', activity => 'sold', object_type => 'music', objectname=> 'Guitar')
    );
aasort($all_users_activities,"username");