0

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
                        )

                )

        )

)
2
  • 1
    Did you give it a try...?? If yes, can you show us your code...?? Commented Feb 11, 2015 at 18:13
  • HI yes i've been trying all afternoon, I have tried so many options i'm not sure where to start... i have tried splitting them into separate arrays then trying to merge back together, i've tried using comparing next and current, recursive array merging, but i'm really a designer who dabbles in code to get to where he needs and i'm a little out of my depth, sorry. Commented Feb 11, 2015 at 18:21

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.