Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Combing Array Elements Across Different Arrays
Old 12-05-2008, 10:39 AM Combing Array Elements Across Different Arrays
Junior Talker

Posts: 2
Name: Shane Jeffery
Trades: 0
Hey there guys.

I am trying to combine two or more arrays based on values that match within those arrays.

Here is an example of the array I am working with:

Code:
    [5] => Array
        (
            [id] => 207
            [session_id] => 6589
            [conference] => 198
            [title] => (206) Studies of Threading Successes in Popular PC Games and Engines
            [format] => Tutorial
            [type] => 3
            [overview] => Half-day tutorial, repeated in morning and after lunch. In this follow-up to 2007's popular Threading Games for Performance tutorial, Paul Lindberg and Brad Werth will present detailed case studies of successful threading practices in modern PC games. Each case study will be bookended by an explanation of the theory and a "what if" discussion of alternative approaches and further improvements. In addition to detail on specific games, major PC engines will be covered -- maybe the one you are planning to use in your next game.
            [file_name] => o1/vault/gdc08/slides/S6589i3.ppt
            [ftp_url] => ftp://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6589i3.ppt
            [http_url] => http://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6589i3.ppt
            [featured] => 0
            [free] => 0
            [homepage] => 
            [image] => 
            [speakers] => Array
                (
                    [0] => Array
                        (
                            [id] => 285
                            [cmpevents_id] => 442854
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=442854
                            [first_name] => Paul
                            [last_name] => Lindberg
                            [job_title] => Senior Software Engineer
                            [company] => Intel
                        )

                    [1] => Array
                        (
                            [id] => 254
                            [cmpevents_id] => 550518
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=550518
                            [first_name] => Brad
                            [last_name] => Werth
                            [job_title] => Senior Software Engineer
                            [company] => Intel
                        )

                )

            [sort_by_speaker] => Paul Lindberg
            [sort_by_company] => Intel
            [tracks] => Array
                (
                    [0] => Array
                        (
                            [id] => 23
                            [name] => Programming
                            [conference] => 198
                        )

                )

            [tracks_display] => Programming
            [type_data] => Array
                (
                    [id] => 3
                    [name] => Slides
                    [icon_url] => /vault/images/icon_slideshow.gif
                )

            [rating] => Array
                (
                    [class] => rating nostar
                )

        )

    [6] => Array
        (
            [id] => 208
            [session_id] => 6589
            [conference] => 198
            [title] => (206) Studies of Threading Successes in Popular PC Games and Engines
            [format] => Tutorial
            [type] => 3
            [overview] => Half-day tutorial, repeated in morning and after lunch. In this follow-up to 2007's popular Threading Games for Performance tutorial, Paul Lindberg and Brad Werth will present detailed case studies of successful threading practices in modern PC games. Each case study will be bookended by an explanation of the theory and a "what if" discussion of alternative approaches and further improvements. In addition to detail on specific games, major PC engines will be covered -- maybe the one you are planning to use in your next game.
            [file_name] => o1/vault/gdc08/slides/S6589i4.ppt
            [ftp_url] => ftp://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6589i4.ppt
            [http_url] => http://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6589i4.ppt
            [featured] => 0
            [free] => 0
            [homepage] => 
            [image] => 
            [speakers] => Array
                (
                    [0] => Array
                        (
                            [id] => 285
                            [cmpevents_id] => 442854
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=442854
                            [first_name] => Paul
                            [last_name] => Lindberg
                            [job_title] => Senior Software Engineer
                            [company] => Intel
                        )

                    [1] => Array
                        (
                            [id] => 254
                            [cmpevents_id] => 550518
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=550518
                            [first_name] => Brad
                            [last_name] => Werth
                            [job_title] => Senior Software Engineer
                            [company] => Intel
                        )

                )

            [sort_by_speaker] => Paul Lindberg
            [sort_by_company] => Intel
            [tracks] => Array
                (
                    [0] => Array
                        (
                            [id] => 23
                            [name] => Programming
                            [conference] => 198
                        )

                )

            [tracks_display] => Programming
            [type_data] => Array
                (
                    [id] => 3
                    [name] => Slides
                    [icon_url] => /vault/images/icon_slideshow.gif
                )

            [rating] => Array
                (
                    [class] => rating nostar
                )

        )
Alright, so, here is what needs to happen.

If Array #5 and #6 have the the same session_id and type, I need the file_name, ftp_url, and http_url of Array #6 to be appended to Array #5. All of the values within the arrays are the same minus the file_name, ftp_url, and http_url.

Also, please know that their could be 4-5 matching arrays all with the same session_id and type and they would all need to be merged into *1* entry with their respective file_name, ftp_url, and http_url.

Any ideas on how to do this?
knucklehead00 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-05-2008, 02:45 PM Re: Combing Array Elements Across Different Arrays
Junior Talker

Posts: 2
Name: Shane Jeffery
Trades: 0
Alright I got it VERY close to what I want. Just have one issue.

Code:
function combineArrays($arrays) {
    $newarray = array();
    $cnt = count($arrays);
    for ($i=0; $i < $cnt; $i++) {
        $array = $arrays[$i];
        
        $comparison_found = false;
        $exists_in_array = false;
        
        foreach ($arrays as $nextArray) {
            
            foreach($newarray as $storedarray)
            {    
                if(strpos($storedarray['id'], $nextArray['id']))
                {
                    $exists_in_array = true;
                }
            }
            
            if(!$exists_in_array)
            {
                if($comparison_found == true)
                {
                    $match_existing_array = end($newarray);
                    $compared_array = compareArray($match_existing_array, $nextArray);
                    if($compared_array)
                    {
                        $value_holder = array_pop($newarray);
                        array_push($newarray, $compared_array);
                    }
                    
                }
                else
                {
                    $compared_array = compareArray($array, $nextArray);
                    if (is_array($compared_array))
                    {
                        $newarray[] = $compared_array;
                        
                        $comparison_found = true; 
                    }    
                    else
                    {
                        $comparison_found = false;
                    }
                }
            }
        }
        
        if($comparison_found == false || $exists_in_array == false)
        {
            $newarray[] = $array;
        }
        
        unset($arrays[$i]);
    }
    return $newarray;
}

function compareArray($array1, $array2) {
    if ($array1['session_id'] == $array2['session_id'] && $array1['type'] == $array2['type'] && 
                $array1['id'] != $array2['id']) {    
             
                 $array1['id'] = $array1['id'].", ".$array2['id'];
                 $array1['file_name'] = $array1['file_name'].", ".$array2['file_name'];
                $array1['ftp_url'] = $array1['ftp_url'].", ".$array2['ftp_url'];
                 $array1['http_url'] = $array1['http_url'].", ".$array2['http_url'];
                 $array1['rating']['class'] = $array1['rating']['class'].", ".$array2['rating']['class'];
                 
                 return $array1;
             }
             return $false;
}
The problem I am having now is that I cannot get the duplicate entries to go away.

Here is what is coming out:

Code:
    [1] => Array
        (
            [id] => 180, 179
            [session_id] => 6526
            [conference] => 198
            [title] => (205) Normal Mapping Industry Survey
            [format] => Tutorial
            [type] => 3
            [overview] => This session will provide a broad survey of the methods, tools and process for generating Normal Map Data. Attendees will learn the process for generating high resolution models using such tools as Mudbox and Zbrush, the process for applying the maps in major 3d packages and how these packages work with normal maps, and we'll peak into the other various methods used on projects to generate normal maps without the use of high resolution assets. All materials in the class will be presented by a number of industry leading speakers experienced in the process.
            [file_name] => o1/vault/gdc08/slides/S6526i2.pdf, o1/vault/gdc08/slides/S6526i1.pdf
            [ftp_url] => ftp://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6526i2.pdf, ftp://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6526i1.pdf
            [http_url] => http://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6526i2.pdf, http://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6526i1.pdf
            [featured] => 0
            [free] => 0
            [homepage] => 
            [image] => 
            [speakers] => Array
                (
                    [0] => Array
                        (
                            [id] => 397
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=586066
                            [first_name] => Alex
                            [last_name] => Velasquez
                            [job_title] => 
                            [company] => Raven Software
                        )

                    [1] => Array
                        (
                            [id] => 396
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=581469
                            [first_name] => Ricardo
                            [last_name] => Ariza
                            [job_title] => Character Artist
                            [company] => Naughty Dog
                        )

                    [2] => Array
                        (
                            [id] => 395
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=573755
                            [first_name] => Scott
                            [last_name] => Spencer
                            [job_title] => Digital Art Director
                            [company] => Gentle Giant Studios
                        )

                    [3] => Array
                        (
                            [id] => 394
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=571079
                            [first_name] => Gio
                            [last_name] => Nakpil
                            [job_title] => Artist
                            [company] => Freedom of Teach
                        )

                    [4] => Array
                        (
                            [id] => 393
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=529524
                            [first_name] => Rich
                            [last_name] => Diamant
                            [job_title] => Lead Character Artist
                            [company] => Naughty Dog, Inc.
                        )

                    [5] => Array
                        (
                            [id] => 392
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=524514
                            [first_name] => Adam
                            [last_name] => Myhill
                            [job_title] => Lead Technical Artist
                            [company] => Pandemic Studios
                        )

                    [6] => Array
                        (
                            [id] => 391
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=265346
                            [first_name] => Steve
                            [last_name] => Chapman
                            [job_title] => Vice President
                            [company] => Gentle Giant Studios
                        )

                )

            [sort_by_speaker] => Alex Velasquez
            [sort_by_company] => Raven Software
            [tracks] => Array
                (
                    [0] => Array
                        (
                            [id] => 21
                            [name] => Visual Arts
                            [conference] => 198
                        )

                )

            [tracks_display] => Visual Arts
            [type_data] => Array
                (
                    [id] => 3
                    [name] => Slides
                    [icon_url] => /vault/images/icon_slideshow.gif
                )

            [rating] => Array
                (
                    [class] => rating nostar, rating nostar
                )

        )

    [2] => Array
        (
            [id] => 180
            [session_id] => 6526
            [conference] => 198
            [title] => (205) Normal Mapping Industry Survey
            [format] => Tutorial
            [type] => 3
            [overview] => This session will provide a broad survey of the methods, tools and process for generating Normal Map Data. Attendees will learn the process for generating high resolution models using such tools as Mudbox and Zbrush, the process for applying the maps in major 3d packages and how these packages work with normal maps, and we'll peak into the other various methods used on projects to generate normal maps without the use of high resolution assets. All materials in the class will be presented by a number of industry leading speakers experienced in the process.
            [file_name] => o1/vault/gdc08/slides/S6526i2.pdf
            [ftp_url] => ftp://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6526i2.pdf
            [http_url] => http://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6526i2.pdf
            [featured] => 0
            [free] => 0
            [homepage] => 
            [image] => 
            [speakers] => Array
                (
                    [0] => Array
                        (
                            [id] => 397
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=586066
                            [first_name] => Alex
                            [last_name] => Velasquez
                            [job_title] => 
                            [company] => Raven Software
                        )

                    [1] => Array
                        (
                            [id] => 396
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=581469
                            [first_name] => Ricardo
                            [last_name] => Ariza
                            [job_title] => Character Artist
                            [company] => Naughty Dog
                        )

                    [2] => Array
                        (
                            [id] => 395
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=573755
                            [first_name] => Scott
                            [last_name] => Spencer
                            [job_title] => Digital Art Director
                            [company] => Gentle Giant Studios
                        )

                    [3] => Array
                        (
                            [id] => 394
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=571079
                            [first_name] => Gio
                            [last_name] => Nakpil
                            [job_title] => Artist
                            [company] => Freedom of Teach
                        )

                    [4] => Array
                        (
                            [id] => 393
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=529524
                            [first_name] => Rich
                            [last_name] => Diamant
                            [job_title] => Lead Character Artist
                            [company] => Naughty Dog, Inc.
                        )

                    [5] => Array
                        (
                            [id] => 392
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=524514
                            [first_name] => Adam
                            [last_name] => Myhill
                            [job_title] => Lead Technical Artist
                            [company] => Pandemic Studios
                        )

                    [6] => Array
                        (
                            [id] => 391
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=265346
                            [first_name] => Steve
                            [last_name] => Chapman
                            [job_title] => Vice President
                            [company] => Gentle Giant Studios
                        )

                )

            [sort_by_speaker] => Alex Velasquez
            [sort_by_company] => Raven Software
            [tracks] => Array
                (
                    [0] => Array
                        (
                            [id] => 21
                            [name] => Visual Arts
                            [conference] => 198
                        )

                )

            [tracks_display] => Visual Arts
            [type_data] => Array
                (
                    [id] => 3
                    [name] => Slides
                    [icon_url] => /vault/images/icon_slideshow.gif
                )

            [rating] => Array
                (
                    [class] => rating nostar
                )

        )

    [3] => Array
        (
            [id] => 179
            [session_id] => 6526
            [conference] => 198
            [title] => (205) Normal Mapping Industry Survey
            [format] => Tutorial
            [type] => 3
            [overview] => This session will provide a broad survey of the methods, tools and process for generating Normal Map Data. Attendees will learn the process for generating high resolution models using such tools as Mudbox and Zbrush, the process for applying the maps in major 3d packages and how these packages work with normal maps, and we'll peak into the other various methods used on projects to generate normal maps without the use of high resolution assets. All materials in the class will be presented by a number of industry leading speakers experienced in the process.
            [file_name] => o1/vault/gdc08/slides/S6526i1.pdf
            [ftp_url] => ftp://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6526i1.pdf
            [http_url] => http://cmpmedia.vo.llnwd.net/o1/vault/gdc08/slides/S6526i1.pdf
            [featured] => 0
            [free] => 0
            [homepage] => 
            [image] => 
            [speakers] => Array
                (
                    [0] => Array
                        (
                            [id] => 397
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=586066
                            [first_name] => Alex
                            [last_name] => Velasquez
                            [job_title] => 
                            [company] => Raven Software
                        )

                    [1] => Array
                        (
                            [id] => 396
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=581469
                            [first_name] => Ricardo
                            [last_name] => Ariza
                            [job_title] => Character Artist
                            [company] => Naughty Dog
                        )

                    [2] => Array
                        (
                            [id] => 395
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=573755
                            [first_name] => Scott
                            [last_name] => Spencer
                            [job_title] => Digital Art Director
                            [company] => Gentle Giant Studios
                        )

                    [3] => Array
                        (
                            [id] => 394
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=571079
                            [first_name] => Gio
                            [last_name] => Nakpil
                            [job_title] => Artist
                            [company] => Freedom of Teach
                        )

                    [4] => Array
                        (
                            [id] => 393
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=529524
                            [first_name] => Rich
                            [last_name] => Diamant
                            [job_title] => Lead Character Artist
                            [company] => Naughty Dog, Inc.
                        )

                    [5] => Array
                        (
                            [id] => 392
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=524514
                            [first_name] => Adam
                            [last_name] => Myhill
                            [job_title] => Lead Technical Artist
                            [company] => Pandemic Studios
                        )

                    [6] => Array
                        (
                            [id] => 391
                            [cmpevents_id] => 
                            [link] => https://www.cmpevents.com/GD08/a.asp?option=G&V=3&id=265346
                            [first_name] => Steve
                            [last_name] => Chapman
                            [job_title] => Vice President
                            [company] => Gentle Giant Studios
                        )

                )

            [sort_by_speaker] => Alex Velasquez
            [sort_by_company] => Raven Software
            [tracks] => Array
                (
                    [0] => Array
                        (
                            [id] => 21
                            [name] => Visual Arts
                            [conference] => 198
                        )

                )

            [tracks_display] => Visual Arts
            [type_data] => Array
                (
                    [id] => 3
                    [name] => Slides
                    [icon_url] => /vault/images/icon_slideshow.gif
                )

            [rating] => Array
                (
                    [class] => rating nostar
                )

        )
Now because ID 179 and 180 were already used in Array #2, they should not be getting their own output like shown in Array #3 and Array #4. If the ID 179 and 180 already exist in a previous array, then do not let them create their own arrays.

Any ideas?
knucklehead00 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Combing Array Elements Across Different Arrays
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.79290 seconds with 12 queries