开发者

How to group elements of array using Primary and secondary key?

I've got this array:

Array
(
    [0] => Array
        (
            [id]=>1
            [account_id] => 1
            [object_id] => 43
            [object_type] => PHOTO
            [action_type] => UPLOAD_PHOTO
        )

    [1] => Array
      开发者_如何学JAVA  (
            [id] => 1
            [account_id] => 1
            [object_id] => 42
            [object_type] => PHOTO
            [action_type] => UPLOAD_PHOTO
        )

    [2] => Array
        (
            [id] => 1
            [account_id] => 1
            [object_id] => 41
            [object_type] => PHOTO
            [action_type] => UPLOAD_PHOTO
        )

    [3] => Array
        (
            [id] => 2
            [account_id] => 2
            [object_id] => 1
            [object_type] => USER
            [action_type] => FOLLOW_USER
        )

    [4] => Array
        (
            [id] => 1
            [account_id] => 1
            [object_id] => 2
            [object_type] => USER
            [action_type] => FOLLOW_USER
        )

    [5] => Array
        (
            [id] => 1
            [account_id] => 1
            [object_id] => 1
            [object_type] => PHOTO
            [action_type] => UPLOAD_PHOTO
        )

)

Now I want to group elements have same value (for example UPLOAD_PHOTO) by id as Primary Key and action_type as Secondary Key, like:

Array
(
    [0] => Array(
        [id] => 1
        [account_id] => 1
        [actions] => array(
          [1] => array(
              [object_id] => 42
              [object_type] => PHOTO
              [action_type] => UPLOAD_PHOTO
           )
          [2] => array(
              [object_id] => 43
              [object_type] => PHOTO
              [action_type] => UPLOAD_PHOTO
           )
         )      

 ) 
[2] => Array
    (
        [id] => 1
        [account_id] => 1
        [actions] = array(
            [0] => Array
             (
               [object_id] => 3
               [object_type] => USER
               [action_type] => FOLLOW_USER
             )
            [1] => Array
             (
               [object_id] => 4
               [object_type] => USER
               [action_type] => FOLLOW_USER
             )

        )

    )

)

I tried some solutions but didn't succeed.


When constructing your output array, you'll want to use meaningful array keys. That way you can find the out element that the in element needs to be added to:

$in = (...your array...);
$out = array();

foreach($in as $element) {
   $id = $element['id'];
   //If that id hasn't been seen yet, create the out element.
   if (!isset($out[$id])) {
       $out[$id] = array(
           'id'=>$element['id'],
           'account_id' => $element['account_id'],
           'actions' => array()
       );
   }

   //Add that action to the out element
   $out[$id]['actions'][] = array(
       'object_id' => $element['object_id'],
       'object_type' => $element['object_type'],
       'action_type' => $element['action_type']
   );
}

I'm not sure why your input elements have different fields... If this is a desired feature, where one set of possible fields belongs to the id group and another set belongs to the action, you can do this instead (slightly less readable, but more flexible):

$in = (...your array...);
$out = array();
//These define which fields from an 
//input element belong to the id,
//and which to the action.
$id_fields = array_flip(array('id','account_id','object_user_id');
$action_fields = array_flip(array('object_id','object_type','action_type');

foreach($in as $element) {
   $id = $element['id'];
   //If that id hasn't been seen yet, create the out element.
   if (!isset($out[$id])) {
       $out[$id] = array_intersect_key($element, $id_fields);
       $out[$id]['actions'] = array();
   }

   //Add that action to the out element
   $out[$id]['actions'][] = array_intersect_key($element, $action_fields);
}

This is a better solution if the input elements can be different, because if an expected field (other than 'id') is missing, the script will cope with it easily.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜