#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    206
    Rep Power
    11

    Sorting multidimensional array


    I have a muli-dimensional array (see below).

    I'm trying to use PHP to group matching events. Basically any matchname that is a duplicate (eg. Reds vs Whites) to group them together and group their linksets...

    Code:
    Array
    (
        [matches] => Array
            (
                [match] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [sportname] => Football
                                [tournamentname] => Crown League
                                [thetime] => 201308021600
                                [matchname] => Reds vs Whites
                                [linkset] => SimpleXMLElement Object
                                    (
                                        [link] => link1.html
                                        [link] => link3.html
                                    )
    
    
                            )
    
                        [1] => SimpleXMLElement Object
                            (
                                [sportname] => Football
                                [tournamentname] => Prince League
                                [thetime] => 201308021130
                                [matchname] => Blues vs Yellows
                                [linkset] => SimpleXMLElement Object
                                    (
                                        [link] => link2.html
                                    )
    
                            )
    
                        [2] => SimpleXMLElement Object
                            (
                                [sportname] => SimpleXMLElement Object
                                [tournamentname] => SimpleXMLElement Object
                                [thetime] => 201308020930
                                [matchname] => Reds vs Whites
                               [linkset] => SimpleXMLElement Object
                                    (
                                        [link] => link4.html
                                    )
    
    
                            )
                            
                            
                             )
                            )
                           )
    I am trying to end up with
    Football: Reds vs Whites (201308021600) - Crown League
    Links: link1.html, link3.html, link4.html

    Football: Blues vs Yellows (201308021130) - Prince League
    Links: link2.html

    I've been looking at the PHP manual but tearing my hair out trying to get it to output the above. Any ideas would be most welcome.

    Thanks
    Last edited by denhamd2; September 1st, 2013 at 06:02 AM.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    10
    Rep Power
    0
    Where are you getting the data from? Can you sort it before you put it in that array?
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    You need to use [phpnet=usort]usort[/phpnet] since your innermost element is not an array (they're objects).

    Then, once it's sorted, loop through and aggregate all the LINKS in a temporary array until the item changes, then dump your output, clear your temporary variables, and start again.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    206
    Rep Power
    11
    Hi,

    I'm getting the data from an XML file. I'm not able to sort it, but am using SimpleXML to turn it into an array. I chose to do this because PHP's array functions seem to be better than using SimpleXML for everything.

    So what I've done now is copied the array and combined duplicate events with the same matchname. The problem I'm currently having is trying to get the links from the duplicate event to copy over to the master event. Any ideas on where I might be going wrong here?

    Here is my PHP code:

    PHP Code:
    <?php 
    // Pull in from XML file
        
    $xml simplexml_load_file('test5.xml'); 

    // Use SimpleXML to convert XML to an array
    function xml2array $xmlObject$out = array () ) 

        foreach ( (array) 
    $xmlObject as $index => $node 
            
    $out[$index] = ( is_object $node ) ) ? xml2array $node ) : $node

        return 
    $out

    $array XML2Array($xml); 
    $array = array($xml->getName() => $array); 

    // Create a copy of the above array to combine duplicate events
    $aCopy = array(); 
    foreach (
    $array['matches']['match'] AS $match

        if (!isset(
    $aCopy["{$match->matchname}"]))
        { 
            
    $aCopy["{$match->matchname}"] = $match
        } 
        else 
        { 
            foreach (
    $match->linkset AS $link
            { 
            echo 
    $link;
                
    $aCopy["{$match->matchname}"]->linkset->addChild('link'$link); // not working - the links of the duplicate matches are not copying over to the master matches
            

        } 
    }   

    // Print out the copied array
    print_r($aCopy); 

    ?>
    Here is the copied array (Its missing link4.html)

    PHP Code:
    Array
    (
        [
    Reds vs Whites] => SimpleXMLElement Object
            
    (
                [
    sportname] => Football
                
    [tournamentname] => Crown League
                
    [thetime] => 201308021600
                
    [matchname] => Reds vs Whites
                
    [linkset] => SimpleXMLElement Object
                    
    (
                        [
    link] => Array
                            (
                                [
    0] => link1.html>
                                [
    1] => link3.html
                                
    [2] => SimpleXMLElement Object
                                    
    (
                                    )

                            )

                    )

            )

        [
    Blues vs Yellows] => SimpleXMLElement Object
            
    (
                [
    sportname] => Football
                
    [tournamentname] => Crown League
                
    [thetime] => 201308021130
                
    [matchname] => Blues vs Yellows
                
    [linkset] => SimpleXMLElement Object
                    
    (
                        [
    link] => link2.html
                    
    )

            )


    Any ideas?
    Last edited by denhamd2; September 2nd, 2013 at 04:12 AM.
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    I don't know how your output array STILL has SimpleXML elements in it. Seems like your xml2array function isn't working as designed.

    $aCopy["{$match->matchname}"]->linkset->addChild('link', $link);

    That line even assumes you're dealing with an xml object when you're trying to deal with arrays. Make this all arrays first, then work on the merge.

    Good use of recursion in your xml translation function btw.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo