简体   繁体   English

如何使用主键和副键对数组元素进行分组?

[英]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
        (
            [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: 现在,我想将具有相同值的元素(例如UPLOAD_PHOTO )按id UPLOAD_PHOTO为Primary Key,将action_type分组为Secondary Key,例如:

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元素添加到的out元素:

$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): 我不确定为什么您的输入元素具有不同的字段...如果这是一项理想功能,其中一组可能的字段属于id组,而另一组可能属于动作,则可以这样做(可读性稍差,但更灵活):

$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. 如果输入元素可以不同,则这是一个更好的解决方案,因为如果缺少期望的字段(不是'id' ),脚本将轻松应对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM