Re: find duplicated values in array and count them

From: Date: Thu, 11 Nov 2004 13:29:25 +0000
Subject: Re: find duplicated values in array and count them
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-201763@lists.php.net to get a copy of this message
Patrick Fehr wrote:
Hi all I need to compare values of multiple arrays, the tricky thing: I can't just use array_diff, because I also need to count all the occurences and give them back. The more tricky thing: It's a foreach loop through $arr[x ['comparable value'] while x is 1,4,3,6... so it's not that easy to compare one item in the array under index 4 with the next one(which is in 3) because foreach just loops. The idea is: If two keys of different arrays hold the same value, I want to just show one entry and tell the same time, how often it occurs(in other arrays), so I don't show all the entries(as I would, if they weren't the same. But it is still a bit more complicated, because: only if the date_s value lies 7 days from each other, I want to "concatenate" them to one entry + show the number of occurences. In the example below, the lessons 18 and 15 fit that need. so I want them to be shown as:
          [room] : Raum1
          [teacher] : Marianne
          [class] : SundayClass
          starttime : 1099404000 (which is Array[19][date_s])
          endtime : 099418400 + 7*24*60*60 (which is Array[15][date_e])
          occurences : 2
       eg ---------->
print_r($my_big_array) shows: Array (
    [7] => Array
        (
            [name] => course1
            [comment] => my bad comment
            [lessons] => Array
                (
                    [14] => Array
                        (
                            [room] => Raum2
                            [teacher] => Andrew
                            [class] => Team2
                            [date_s] => 1100796900
                            [date_e] => 1100805000
                        )
                )
        )
    [3] => Array
        (
            [name] => test
            [comment] => testing
            [lessons] => Array
                (
                    [19] => Array
                        (
                            [room] => Raum1
                            [teacher] => Marianne
                            [class] => SundayClass
                            [date_s] => 1099404000
                            [date_e] => 1099418400
                        )
                    [15] => Array
                        (
                            [room] => Raum1
                            [teacher] => Marianne
                            [class] => SundayClass
                            [date_s] => 1099404000 + 7*24*60*60
                            [date_e] => 1099418400 + 7*24*60*60
                        )
                    [13] => Array
                        (
                            [room] => Raum1
                            [teacher] => Peter
                            [class] => Team1
                            [date_s] => 1100798160
                            [date_e] => 1100803500
                        )
                )
        )
loop through all and create a new array foir counting foreach ( your arrays to serach for as $array) { counts[$array['room']][$array['teacher']][$array['class']]++; // or by whatever is relevant for you } -- Sebastian Mendel www.sebastianmendel.de www.warzonez.de www.tekkno4u.de www.nofetish.com www.sf.net/projects/phpdatetime www.sf.net/projects/phptimesheet

« previous php.general (#201763) next »