I am trying to save node values to an array. This is the XML structure:
<wildcards>
<conference id="1">
<seeds>
<seed divId="1">10,7,17</seed>
<seed divId="2">8,5,3</seed>
</seeds>
<wild>2,4</wild>
<elimination>11,6,14,1,13,12,20,15</elimination>
</conference>
</wildcards>
The xml file can be found here
I want to associate the node values of conferences 1 with the array key being what place they are in the wildcard standings. Here is my code:
$xmlDoc = new DOMDocument();
$xmlDoc->load('http://www.tsn.ca/datafiles/XML/NHL/standings.xml');
$searchNode = $xmlDoc->getElementsByTagName('wildcards');
foreach ($searchNode as $node) {
$conference1 = $node->getElementsByTagName('conference');
$conference1 = $conference1->item(0)->nodeValue;
}
echo $conference1;
// $conference1 = str_replace(" ",",",$conference1);
$conference1Array = explode(',', $conference1);
$conference1ArrayLength = count($conference1Array) ;
for ($i = 1; $i < $conference1ArrayLength; $i++) {
echo $i.": ".$conference1Array[$i-1];
echo "<br/>";
}
If you go to this website, you can see my current output. I tried to string replace whitespace and explode on ',' to get the array correct, but it did not work.
The output of conference1 is: 10,7,17 8,5,3 2,4 11,6,14,1,13,12,20,15
. Even though there is whitespace the replace did not work.
I need to have a comma after 17, 3 and 4. The idea is to insert the key for the team id into a column in my database named seed
. I can then use PHP to determine what numbers are associated with each division / wildcard for table output. Here is what I mean by the association of the key and the team id / node values:
Team Id: 10 -> 1 (first in division 1)
Team Id: 7 -> 2 (second in division 1)
Team Id: 17 -> 3 (third in division 1)
Team Id: 8 -> 4 (1st in division 2)
Team Id: 5 -> 5 (2nd in division 2)
Team Id: 3 -> 6 (3rd in division 3)
Team Id: 2 -> 7 (Wildcard 1)
Team Id: 4 -> 8 (Wildcard 2)
Team Id: all the rest (eliminated)
I tried to get node values separately using code from this link, but I could not figure out how to get it to work with my situation. I don't have my attempted code for that anymore, by the way.
Edit: I have looked at the links provided and have come up with this:
$searchNode = $xmlDoc->getElementsByTagName('wildcards');
$divData = array();
foreach($searchNode as $node){
foreach($node->childNodes as $child) {
foreach($child->childNodes as $secondChild) {
foreach($secondChild->childNodes as $thirdChild) {
$divData[] = array($thirdChild->nodeName => $thirdChild->nodeValue);
}
}
}
}
The output of conference1 is: 10,7,17 8,5,3 2,4 11,6,14,1,13,12,20,15. Even though there is whitespace the replace did not work.
You are almost there, use the following code to insert commas:
$conference1 = '10,7,17 8,5,3 2,4 11,6,14,1,13,12,20,15 ';
$conference1 = str_replace(" ",",",trim($conference1));
UPDATE 1
Try this to replace any characters tha are not comma or [0-9]
$new_string = preg_replace('/[^0-9,]+/u', ",", $your_string);
UPDATE 2
To fix extra commas at begin and end, do another trim $new_string = trim($your_string,",");