PHP 将 2d 数组排序为 3d 数组

发布于 2024-09-25 04:16:53 字数 844 浏览 0 评论 0原文

我试图弄清楚如何在 PHP 中将 2d 数组排序为 3d 数组。该数组包含管理树中用户的任务,目前的排列方式如下:

Array ( [0] => Array ( [Title] => Test Task [Author] => 5 [DateIn] => 2010-09-15 [Deadline] => 2010-09-30 [Position] => 1 [Description] => This is a test task [Assignee] => 3 ) [1] => Array ( [Title] => Test Task [Author] => 5 [DateIn] => 2010-09-15 [Deadline] => 2010-09-29 [Position] => 1 [Description] => Blah blah [Assignee] => 3 ) ) 

我想对其进行排序,使其成为一个 3d 数组,其中每个第二级数组包含分配给一个用户的所有任务。有没有简单的方法可以做到这一点?我发现的最接近的是 array_multsort,但这并不能完全满足我的要求。

*编辑 这基本上是我目前工作的公司的任务管理器应用程序。经理需要能够查看已分配给员工的任务。我目前将员工和主管组织成一棵树,因此他们需要查看其分支中的所有内容。

现在,我编写的函数返回每个“经理”的员工的任务,不按特定顺序排序。为了减少混乱,我需要一次只能显示一个“员工”的任务。目前这是不可能的,因为数组的结构仅包含作者被标记为当前用户的所有任务的列表。

简而言之,我想将这些任务排序到一个 3 维数组中,其中每个二级数组包含属于当前用户的一名“员工”的任务。

I'm trying to figure out how to sort a 2d array into a 3d array in PHP. The array contains tasks from users in a management tree and is currently arranged like so:

Array ( [0] => Array ( [Title] => Test Task [Author] => 5 [DateIn] => 2010-09-15 [Deadline] => 2010-09-30 [Position] => 1 [Description] => This is a test task [Assignee] => 3 ) [1] => Array ( [Title] => Test Task [Author] => 5 [DateIn] => 2010-09-15 [Deadline] => 2010-09-29 [Position] => 1 [Description] => Blah blah [Assignee] => 3 ) ) 

I want to sort it so that it's a 3d array where each 2nd level array contains all of the tasks assigned to one user. Is there an easy way to do this? The closest thing I found was array_multsort, but that doesn't quite do what I want.

*Edits
This is basically a task manager app for the company I currently work for. Managers need to be able to see the tasks that have been assigned to their employees. I currently have employees and supervisors organized into a tree, so they need to see everything in their branch.

Right now, the function I've written returns the tasks for employees of each "manager" sorted in no particular order. To reduce clutter, I need to be able to only display one "employee's" tasks at a time. Currently this is not possible, as the structure of the array is such that it simply contains a list of all the tasks where the author is marked as the current user.

In short, I would like to sort these tasks into a 3 dimensional array, where each 2nd level array contains the tasks belonging to one "employee" belonging to the current user.

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

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

发布评论

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

评论(2

不忘初心 2024-10-02 04:16:53

我不太明白你的意思,但如果你想生成一个新的数组,其中列出受让人的任务,那么你可以使用以下内容。

foreach ( $old as $task )
  $new[$task['Assignee']][] = $task;

这将为您提供一个数组,其中键是受让人 ID,子数组是分配给他们的任务。

I don't quite understand what you're getting at, but if you want to produce a new array which lists tasks by assignee, then you can use the following.

foreach ( $old as $task )
  $new[$task['Assignee']][] = $task;

This will give you an array where the keys are the assignee IDs and the sub arrays are the tasks assigned to them.

荒岛晴空 2024-10-02 04:16:53

因此,如果我理解,您的数组将具有以下排列:

manager1
    employee1
        task1
    employee2
        task2
        task3
manager2
    employee3
.
.
.

这是正确的吗?如果是这样,则假设“作者”是经理,“受让人”是员工,请尝试以下操作:

$new_array = array();
foreach($old_array as $task){
    if(array_key_exists($task["author"], $new_array)){ // the manager already has an array slot
        if(array_key_exists($task["assignee"], $new_array[$task["author"]])){ // the employee already has an array slot under this manager
            array_push($new_array[$task["author"]][$task["assignee"]], $task);
        } else { // manager exists, employee doesn't
            $new_array[$task["author"]][$task["assignee"]][0] = $task;
        }
    } else { // manager doesn't exist
        $new_array[$task["author"]] = array($task["assignee"] => array($task));
    }
}

So if I understand, your array will have the following arrangement:

manager1
    employee1
        task1
    employee2
        task2
        task3
manager2
    employee3
.
.
.

Is this correct? If so, then assuming "author" is the manager and "assignee" is the employee, try the following:

$new_array = array();
foreach($old_array as $task){
    if(array_key_exists($task["author"], $new_array)){ // the manager already has an array slot
        if(array_key_exists($task["assignee"], $new_array[$task["author"]])){ // the employee already has an array slot under this manager
            array_push($new_array[$task["author"]][$task["assignee"]], $task);
        } else { // manager exists, employee doesn't
            $new_array[$task["author"]][$task["assignee"]][0] = $task;
        }
    } else { // manager doesn't exist
        $new_array[$task["author"]] = array($task["assignee"] => array($task));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文