1

这是一种情况:

蒂姆、罗伯和吉姆住在一起。吉姆有一把吉他,他卖给了蒂姆。吉姆用这笔钱买了一双新的时髦鞋。Rob 不小心弄坏了 Tim 的吉他(他是从 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. 活动
4

1 回答 1

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");
于 2012-06-25T08:45:14.973 回答