将PHP数组分组为2个或更多不同的值

时间:2011-07-20 20:58:49

标签: php arrays

我正在创建一个最近的活动部分,我正在尝试对类似的项目进行分组。我希望能够根据活动类型和执行活动的人对活动进行分组。例如:

Array
(
    [0] => Array
        (
            [user] => Bill
            [type] => photo
            [id] => 1
            [timestamp] => 12345678
        )

    [1] => Array
        (
            [user] => Joe
            [type] => photo
            [id] => 2
            [timestamp] => 12345678
        )

    [2] => Array
        (
            [user] => Bill
            [type] => comment
            [id] => 1
            [timestamp] => 12345678
        )

    [3] => Array
        (
            [user] => Bill
            [type] => photo
            [id] => 3
            [timestamp] => 12345678
        )

)

可能变成这个:

Array
(
    [0] => Array
        (
            [user] => Bill
            [type] => photo
            [items] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [timestamp] => 12345678
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [timestamp] => 12345678
                        )

                )

        )

    [1] => Array
        (
            [user] => Joe
            [type] => photo
            [items] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [timestamp] => 12345678
                        )

                )

        )

    [2] => Array
        (
            [user] => Bill
            [type] => comment
            [items] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [timestamp] => 12345678
                        )

                )

        )

)

这几乎就是我想要做的但不完全在那里:Grouping arrays in PHP

有人有想法吗?

2 个答案:

答案 0 :(得分:1)

你的分组并没有多大意义(在编辑之后会更有意义),试试这个:

foreach ($activity as $data) {
   $newdata[$data['user']][$data['type']]['id'][]=$data['id'];
   $newdata[$data['user']][$data['type']]['timestamp'][]=$data['timestamp'];
}

编辑以反映所需输出的更改:(以及更有用的结构)

foreach ($activity as $data) {
    $newdata[$data['user']][$data['type']]['items'][]=array('id'=>$data['id'],'timestamp'=>$data['timestamp']);
}  

编辑2:

你必须有某种键,在这种情况下它是用户名和类型的组合。然后你必须重新索引它所以它的数字,如果这实际上很重要。这应该非常接近你想要的:

foreach ($activity as $data) {
        $key=$data['user'].'-'.$data['type'];
        $newdata[$key]['items'][]=array('id'=>$data['id'],'timestamp'=>$data['timestamp']);
        $newdata[$key]['user']=$data['user'];
        $newdata[$key]['type']=$data['type'];
}
$newdata=array_values($newdata);

<强>测试
http://www.ideone.com/3uZdd

编辑3:

单独的日期......这是让你开始的想法,你需要为原始阵列做更多的事情。

foreach ($activity as $data) {
        $key=$data['user'].'-'.$data['type'];
        $newdata[$key]['items'][$data['day']][]=array('id'=>$data['id'],'timestamp'=>$data['timestamp']);
        $newdata[$key]['user']=$data['user'];
        $newdata[$key]['type']=$data['type'];
}
$newdata=array_values($newdata);

答案 1 :(得分:0)

尝试将uasort与自定义函数结合使用,该函数对usertype的值进行排序。