将相似的元素合并成一个多维 PHP 数组

Merge similar elements into a multi-dimensional PHP array

所以我有一个具有相似元素的数组,我需要将它合并到一个多维数组中,将一些元素合并在一起,我可能对生活要求太多,但我想我会问。 .. 提前致谢:)

我当前的数组:

    Array
(
    [0] => Array
        (
            [name] => Facebook
            [icon] => 
            [sectors] => BSS
            [url] => http://www.facebook.com/...
        )

    [1] => Array
        (
            [name] => Facebook
            [icon] => 
            [sectors] => BSP
            [url] => http://www.facebook.com/...
        )

    [2] => Array
        (
            [name] => GooglePlus
            [icon] => 
            [sectors] => BSP
            [url] => https://plus.google.com/...
        )

    [3] => Array
        (
            [name] => LinkedIn
            [icon] => 
            [sectors] => BSS
            [url] => http://www.linkedin.com/...
        )

    [4] => Array
        (
            [name] => LinkedIn
            [icon] => 
            [sectors] => BSP
            [url] => http://www.linkedin.com/...
        )

    [5] => Array
        (
            [name] => Twitter
            [icon] => 
            [sectors] => BSS
            [url] => http://twitter.com/...
        )

    [6] => Array
        (
            [name] => Twitter
            [icon] => 
            [sectors] => BSP
            [url] => http://twitter.com/...
        )

    [7] => Array
        (
            [name] => Vimeo
            [icon] => 
            [sectors] => BSS
            [url] => http://vimeo.com/...
        )

    [8] => Array
        (
            [name] => Vimeo
            [icon] => 
            [sectors] => BSP
            [url] => https://vimeo.com/...
        )

    [9] => Array
        (
            [name] => Youtube
            [icon] => 
            [sectors] => BSS
            [url] => http://www.youtube.com/...
        )

    [10] => Array
        (
            [name] => Blog
            [icon] => 
            [sectors] => Local
            [url] => /blog
        )

)

我需要结束:

Array
(
    [0] => Array
        (
            [name] => Facebook
            [icon] => 
            [sectors] => Array
                (
                    [0] => Array
                        (
                            [name] => BSS
                            [url] => http://www.facebook.com/...
                        )
                    [1] => Array
                        (
                            [name] => BSP
                            [url] => http://www.facebook.com/...
                        )

                )

        )

    [1] => Array
        (
            [name] => GooglePlus
            [icon] => 
            [sectors] => Array
                (
                    [0] => Array
                        (
                            [name] => BSP
                            [url] => https://plus.google.com/...
                        )

                )

        )

    [2] => Array
        (
            [name] => LinkedIn
            [icon] => 
            [sectors] => Array
                (
                    [0] => Array
                        (
                            [name] => BSS
                            [url] => http://www.linkedin.com/....
                        )
                    [1] => Array
                        (
                            [name] => BSP
                            [url] => http://www.linkedin.com/...
                        )

                )

        )

    [3] => Array
        (
            [name] => Twitter
            [icon] => 
            [sectors] => Array
                (
                    [0] => Array
                        (
                            [name] => BSS
                            [url] => http://twitter.com/...
                        )
                    [1] => Array
                        (
                            [name] => BSP
                            [url] => http://twitter.com/...
                        )

                )

        )

    [4] => Array
        (
            [name] => Vimeo
            [icon] => 
            [sectors] => Array
                (
                    [0] => Array
                        (
                            [name] => BSS
                            [url] => http://vimeo.com/...
                        )
                    [1] => Array
                        (
                            [name] => BSP
                            [url] => https://vimeo.com/...
                        )

                )

        )

    [5] => Array
        (
            [name] => Youtube
            [icon] => 
            [sectors] => Array
                (
                    [0] => Array
                        (
                            [name] => BSS
                            [url] => http://www.youtube.com/....
                        )

                )

        )

    [6] => Array
        (
            [name] => Blog
            [icon] => 
            [sectors] => Array
                (
                    [0] => Array
                        (
                            [name] => Local
                            [url] => /blog
                        )

                )

        )

)

我会这样做:

foreach ($original_array as $value){
    $result_array[$value['name']]['name'] = $value['name']; 
    $result_array[$value['name']]['icon'] = $value['icon'];
    $result_array[$value['name']]['sectors'][] = array(
                                                       'name' => $value['sectors'],
                                                       'url' => $value['url']
}

我正在做的是使用名称(Facebook、Youtube 等)作为数组键,以便能够推送(使用 [])子数组的扇区和 url 元素进入他们自己的子阵列。

从这里您可以按原样 return $result_array 或者如果您确实需要索引数组,return array_values($result_array)

我假设图标不会在具有相同名称的两个条目之间改变。如果不是这种情况,您可以对名称 + 图标进行散列,或者只是连接名称和图标并将其用作数组键。