0

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);
            }
        }
    }
}

This is my new output

1 Answer 1

1

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,",");

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry, that doesn't work. My post explicitly states this (and I even have that line of code commented out). I tried it with trim as well. I am guessing the white space is not recognized because between numbers such as 17 and 8, there is a change in nodes.
With trim this is the output 10,7,17 ,,,,,,,,,,,,8,5,3 ,,,,,,,,, ,,,,,,,,,2,4 ,,,,,,,,,11,6,14,1,13,12,20,15
I would need something to limit the commas. With regex, this is the output ,,,,,,,,,,,,,,,,,,,,,,,10,7,17,,,,,,,,,,,,,8,5,3,,,,,,,,,,,,,,,,,,,,2,4,,,,,,,,,,11,6,14,1,13,12,20,15,,,,,,,
Try preg_replace('/[^0-9,]+/u', ",", trim($your_string));
Now it is close. ,10,7,17,8,5,3,2,4,11,6,14,1,13,12,20,15, just need the first and last comma gone. I can figure that one out on my own. Thanks for the answer. I will mark this correct.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.