简体   繁体   English

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

[英]Sort multidimensional Array, merge by keys in PHP

I have this array: 我有这个数组:

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
                )

        )
)

I want to sort it something like this (don't know if i got it exactly right): 我想对它进行排序(不知道我是否正确):

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
                    ) 
            )
    )

So that the matchedMovie arrays are under the same name. 以便matchMovie数组使用相同的名称。 How should i do this? 我应该怎么做?

I have tried this function: 我已经尝试过此功能:

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

But that doesn't work. 但这是行不通的。

Just a quick stab at it. 只是快速刺中它。

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);
}

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

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