Jump to content

Recommended Posts

Hi Guys, I would like to add an Order number/s to the parsed xml code below.

 

<game id="3664066" date="20090325" time="1700">
    <description>
      <category id="SOCMENEURESPSEGB1" order="7200">Spa. Segunda B1</category>Celta B - Valladolid B</description>
    <type id="0"/>
    <alternatives>
      <alternative odds="1.50" team="133702">1</alternative>
      <alternative odds="3.50" team="133702">X</alternative>
      <alternative odds="5.95" team="193111">2</alternative>
    </alternatives>
  </game>
  <game id="3664073" date="20090325" time="1700">
    <description>
      <category id="SOCMENEURESPSEGB1" order="7200">Spa. Segunda B1</category>Deportivo B - Athletic B</description>
    <type id="0"/>
    <alternatives>
      <alternative odds="2.70" team="133707">1</alternative>
      <alternative odds="2.85" team="133707">X</alternative>
      <alternative odds="2.50" team="193120">2</alternative>
    </alternatives>
  </game>

 

 

Here is my code:

 

$xml = new SimpleXMLElement($source,null,true);


foreach($xml->game as $game) {
  $game_id = $game['id'];
     foreach($game->type as $type){
      $type_id = $type['id'];
        foreach($game->alternatives->alternative as $alt_odd){
          $odds = $alt_odd['odds'];
          $team = $alt_odd['team'];

    echo "{$game_id} - {$odds} - {$team} - {$alt_odd} -  {$type_id}<br>";
        }

    }
}

?>

 

And here is the output:

 

3664066 - 1.50 - 133702 - 1 - 0
3664066 - 3.50 - 133702 - X - 0
3664066 - 5.95 - 193111 - 2 - 0
3664073 - 2.70 - 133707 - 1 - 0
3664073 - 2.85 - 133707 - X - 0
3664073 - 2.50 - 193120 - 2 - 0

 

As you can see there are two game_id's 3664066 and 3664073  respectively. These games however have 3 alternatives 1 or X or 2.

 

I would like to add an order to each game id so it should look something like this:

 

 

3664066 - 1.50 - 133702 - 1 - 0 - 1
3664066 - 3.50 - 133702 - X - 0 - 2
3664066 - 5.95 - 193111 - 2 - 0 - 3
3664073 - 2.70 - 133707 - 1 - 0 - 1
3664073 - 2.85 - 133707 - X - 0 - 2
3664073 - 2.50 - 193120 - 2 - 0 - 3 

 

Where the order starts over again for each new game id.

 

Any tips would be greatly appreciated.

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/151880-adding-order-to-loop/
Share on other sites

Hi Guys,

 

I am still stuck on this and was thinking.

 

This is a print_f  of $alt_odd

 

SimpleXMLElement Object           
(                                 
    [@attributes] => Array        
        (                         
            [odds] => 1.50        
            [team] => 133702      
        )                         

    [0] => 1
)           
SimpleXMLElement Object
(                      
    [@attributes] => Array
        (                 
            [odds] => 3.50
            [team] => 133702
        )                   

    [0] => X
)           
SimpleXMLElement Object
(                      
    [@attributes] => Array
        (
            [odds] => 5.95
            [team] => 193111
        )

    [0] => 2
)

 

 

Is there any way i can get and compare the next values of the array

 

something like this:


  if ($game_id == $game_id and $array_value != $array_next_value)
    {
    $i++;
    }

           

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/151880-adding-order-to-loop/#findComment-797878
Share on other sites

array

 

Take a look at the array functions, notably next. That will, however, move the array pointer. So you may have to use previous after you assign the next values to set the array pointer to what you expect it to be.

 

Thanks, will look into it  ;)

Link to comment
https://forums.phpfreaks.com/topic/151880-adding-order-to-loop/#findComment-797896
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.