OnsiteDesigner
OnsiteDesigner

Reputation: 15

Merge similar elements into a multi-dimensional PHP array

so i have an array that has similar elements and i need to merge it into a multi-dimensional array, merging some of the elements together, I maybe asking too much out of life, but thought i'd ask... thanks in advance :)

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

)

I need to end up with:

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
                        )

                )

        )

)

Upvotes: 0

Views: 36

Answers (1)

frost287
frost287

Reputation: 81

Here's what I would do:

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']
}

What I'm doing is using the name (Facebook, Youtube, etc..) as the array key to be able to push (using []) the sectors and url elements of the sub arrays into their own subarray.

From here you can either return $result_array as is or if you really need an indexed array, return array_values($result_array)

I'm making the assumption that icons will not change between two entries that bear the same name. If this is not the case, you can either make a hash of the name + icon or just concatenate the name and icon and use that as the array key.

Upvotes: 1

Related Questions