June 29, 2012

awkward duplication in 2d array php

Question by shanahobo86

I merged two arrays together that both contained a string(url) and int(score). the following is a sample of the outome. Whenever a string is duplicated, i need to remove that string and its corresponding int. For example, on the 4th line (www.thebeatles.com/ – 30) should be removed. The 5th and 6th lines should also be removed as they appear already with a different score.

http://www.thebeatles.com/ - 55
http://en.wikipedia.org/wiki/The_Beatles - 49
http://www.beatlesstory.com/ - 45
http://www.thebeatles.com/ - 30
http://en.wikipedia.org/wiki/The_Beatles - 28
http://www.beatlesstory.com/ - 26
http://www.beatlesagain.com/ - 24
http://www.thebeatlesrockband.com/ - 23
http://www.last.fm/music/The+Beatles - 22
http://itunes.apple.com/us/artist/the-beatles/id136975 - 20
http://www.youtube.com/watch?v=U6tV11acSRk - 18
http://blekko.com/ws/http://www.thebeatles.com/+/seo - 17
http://www.adriandenning.co.uk/beatles.html - 16
http://www.npr.org/artists/15229570/the-beatles - 15
http://mp3.com/artist/The%2BBeatles - 14
http://www.beatles.com/ - 13
http://www.youtube.com/watch?v=TU7JjJJZi1Q - 12
http://www.guardian.co.uk/music/thebeatles - 11
http://www.cirquedusoleil.com/en/shows/love/default.aspx - 9
http://www.recordingthebeatles.com/ - 7
http://www.beatlesbible.com/ - 5

I’m new to PHP and my best efforts to get array_unique() to work have failed. Really appreciate some help guys!

Answer by Sven

Here is a function that merges two arrays and discards any duplications, hopes it helps:

        function merge_links($arr_l, $arr_r) {
            $new_links = array();
            $links = array_merge($arr_l, $arr_r); //the big list with every links


            foreach($links as $link) {
                $found = false; //did we found a duplicate?

                //check if we already have it
                foreach($new_links as $new_link) {
                    if($new_link['url'] == $link['url']) {
                        //duplicate
                        $found = true;

                        break;
                    }
                }

                //not found, so insert it
                if(!$found) {
                    $new_links[] = $link;
                }
            }

            return $new_links;
        }

        $arr1[0]['url'] = 'http://test.nl';
        $arr1[0]['score'] = 30;

        $arr1[1]['url'] = 'http://www.google.nl';
        $arr1[1]['score'] = 30;

        $arr2[0]['url'] = 'http://www.tres.nl';
        $arr2[0]['score'] = 30;

        $arr2[1]['url'] = 'http://test.nl';
        $arr2[1]['score'] = 30;

        print_r(merge_links($arr1, $arr2));

Answer by Starx

Well, even technically, those strings are not unique. i.e. They are completely different.

So, array_unique() will not give you the required output. One way of solving this issue is by defining a separate array and storing the URI and the number separately. A manageable form would be this.

array(

    array("http://www.thebeatles.com", 55),
    array("http://www.thebeatles.com", 30)

);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!