1

Here is a situation:

Tim, Rob and Jim live togather. Jim had a guitar, which he sold to Tim. Jim bought a new pair of fashionable shoes by this money. Rob accidently broke Tim's guitar (which he bought from Tim), After this, Rob gifted angry Tim a toy guitar, which Tim took no time to break.

Here is the array of activities that had been done in the room, lastest activity first.

$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')
    );

Now, I want to sort this data according to:

  1. username
  2. objectname (where 'object_type' is same: 'toy''guitar' is different from 'music''guitar')
  3. activity

1 Answers1

0

Check this out

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");
Wazy
  • 8,449
  • 8
  • 50
  • 95