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

发布于 2024-12-08 06:30:25 字数 2438 浏览 0 评论 0原文

我有这个数组:

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
        )

)

现在我想通过 id 作为主键和 action_type 将具有相同值(例如 UPLOAD_PHOTO)的元素分组为辅助键,例如:

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'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:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-12-15 06:30:25

构建输出数组时,您需要使用有意义的数组键。这样您就可以找到需要添加 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']
   );
}

我不确定为什么您的输入元素具有不同的字段...如果这是一个所需的功能,其中一组可能的字段属于 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);
}

如果输入元素可以不同,这是一个更好的解决方案,因为如果预期字段('id 除外) ') 丢失,脚本会处理它 容易地。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文