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

    Join Date
    Jul 2004
    Posts
    206
    Rep Power
    10

    Matching events in an array


    Hi,

    I have an XML files of sports events (see below). Some of the events have the same match name, so I've put everything into an array and then tried to match them using matchname as a key.


    The XML looks like this:

    Code:
    <match><sportname>Football</sportname><tournamentname>Red League</tournamentname><thetime>201311021245</thetime><matchname dateandtime="1383396300">Town Rovers vs Chanel</matchname><linkset><link lang="en" kbps="300" channelname="Link 1">link1.html</link><link lang="en" kbps="350" channelname="Link 2">link2.html</link></linkset></match>
    
    <match><sportname>Football</sportname><tournamentname>Red League</tournamentname><thetime>201311021245</thetime><matchname dateandtime="1383396300">Town Rovers vs Chanel</matchname><linkset><link lang="en" kbps="320" channelname="Link 3">link3c.html</link><link lang="en" kbps="320" channelname="Link 3">link4.html</link></linkset></match>
    Here's my PHP code (I'm pulling in the XML file using SimpleXML)...

    Code:
    $xml = simplexml_load_file('test3.xml'); 
    $aCopy = array();
    
    foreach ($xml->match AS $match)
    {
        if (!isset($aCopy[(string)$match->matchname]))
        {
            $aCopy[(string)$match->matchname] = $match;
        }
        else
        {
            foreach ($match->linkset->link AS $link)
            {
                $aCopy[(string)$match->matchname]->linkset->addChild('link', $link);
            }
        }
    }

    The code above seems to have gotten me 80% of the way in that they are matching ok and the links output ok. But the weird thing is the attributes on the 2nd instance of the match aren't outptting...

    Code:
    Link 1 en 300 link1.html
    Link 2 en 350 link2.html
    [blank] [blank] [blank] link3.html
    [blank] [blank] [blank] link4.hmtl
    (Where [blank] is supposed to be the channelname, lang and kbps outputted attributes)

    Any suggestions on what I might be doing wrong here?

    Thanks in advance for any help you can offer.
    Last edited by denhamd2; November 2nd, 2013 at 08:20 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    101
    Rep Power
    26
    I think you probably don't need the else-clause there. Remove it so the iteration executes immediately after the end of the if-clause.

IMN logo majestic logo threadwatch logo seochat tools logo