排序多维数组,在PHP中按键合并

时间:2012-12-07 17:59:23

标签: php multidimensional-array merge key

我有这个数组:

Array
(
    [0] => Array
        (
            [name] => Ben
            [matchedMovie] => Array
                (
                    [name] => Saw
                    [genre] => Horror
                    [patheMovie] => Texas Chainsaw 3D
                    [patheMovieGenre] => Horror
                    [score] => 100.00
                )

        )

    [1] => Array
        (
            [name] => Ben 
            [matchedMovie] => Array
                (
                    [name] => Shooter
                    [genre] => Action, Thriller
                    [patheMovie] => The Shining
                    [patheMovieGenre] => Horror, Suspense/Thriller 
                    [score] => 52.38
                )

        )

    [2] => Array
        (
            [name] => Dick
            [matchedMovie] => Array
                (
                    [name] => Resident Evil Movie
                    [genre] => Action/Horror
                    [patheMovie] => Texas Chainsaw 3D
                    [patheMovieGenre] => Horror
                    [score] => 63.16
                )

        )
)

我想对此进行排序(不知道我是否完全正确):

Array
    (
        [0] => Array
            (
                [name] => Ben
                [matchedMovie][0] => Array
                    (
                        [name] => Saw
                        [genre] => Horror
                        [patheMovie] => Texas Chainsaw 3D
                        [patheMovieGenre] => Horror
                        [score] => 100.00
                    )

                [matchedMovie][1] => Array
                    (
                        [name] => Shooter
                        [genre] => Action, Thriller
                        [patheMovie] => The Shining
                        [patheMovieGenre] => Horror, Suspense/Thriller 
                        [score] => 52.38
                    )

            )

        [1] => Array
            (
                [name] => Dick
                [matchedMovie][0] => Array
                    (
                        [name] => Resident Evil Movie
                        [genre] => Action/Horror
                        [patheMovie] => Texas Chainsaw 3D
                        [patheMovieGenre] => Horror
                        [score] => 63.16
                    ) 
            )
    )

这样匹配的Moovie数组名称相同。 我该怎么做?

我尝试过这个功能:

function group_by_key($array) {
        $result = array();
        foreach ($array as $sub) {
            foreach ($sub as $k => $v) {
                $result[$k][] = $v;
            }
        }
        return $result;
    }

但这不起作用。

1 个答案:

答案 0 :(得分:1)

快速刺伤它。

function group_by_key($array){
    $result = array();
    foreach($array as $row){
        if(!isset($result[$row['name']]){
            $result[$row['name']] = array(
                'name'=>$row['name'],
                'matchedMovie'=>array($row['matchedMovie'])
            );
        } else {
            $result[$row['name']]['matchedMovie'][] = $row['matchedMovie'];
        }
    }
    return array_values($result);
}