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]FootballRed League201311021245Town Rovers vs Chanellink1.htmllink2.html

FootballRed League201311021245Town Rovers vs Chanellink3c.htmllink4.html[/CODE]

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);
}
}
} [/CODE]

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…

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.

Are you sure you added all your code? I can’t see anywhere it references the attributes in the linkset.

Also you should wrap the xml in a root element.

[php]<?xml version="1.0" encoding="UTF-8"?>


Football
Red League
201311021245
Town Rovers vs Chanel

link1.html
link2.html



Football
Red League
201311021245
Town Rovers vs Chanel

link3c.html
link4.html


[/php]

Thanks for your reply. Sorry I didnt post the full XML file - the way you posted it is exactly how it looks. I’ve also pasted the exact PHP code below which I’m using to echo the link attributes.

The other strange thing is link4 seems to go totally missing. So firstly, here’s a print out of the array aCopy…

[CODE]Array
(
[Town Rovers vs Chanel] => SimpleXMLElement Object
(
[sportname] => Football
[tournamentname] => Red League
[thetime] => 201311021245
[matchname] => Town Rovers vs Chanel
[linkset] => SimpleXMLElement Object
(
[link] => Array
(
[0] => link1.html
[1] => link2.html
[2] => link3.html
)

            )

    )

)[/CODE]

Here is how I’m outputting the HTML (getting the attributes):

[PHP]foreach ($aCopy AS $thematches)

{

// BUILD THE LINK NAMES FOR THE BASIC STANDALONE PAGES
$singlematchname = strtolower($thematches->matchname); // FORMAT

$singlematchlinks = ‘’;

// LOOP THROUGH ALL LINKS

foreach ($thematches->linkset->link AS $link)
{

        $singlematchlinks .= '<div class="matchrow"><div class="channelname">'.$link["channelname"].'</div><div class="language">'.$link["lang"].'</div><div class="kbps">'.$link["kbps"].'</div><div class="link"><a href="'.$link.'" target="_blank">Watch</a></div><div class="clearb"></div></div>';

}

echo ‘

’.$singlematchname.’
’.$singlematchlinks;
}
[/PHP]

Any idea what I might be doing wrong?

Thread closed, answered on another forum.

Sponsor our Newsletter | Privacy Policy | Terms of Service