Jump to content

Create an array based on array key


stublackett

Recommended Posts

Hi,
 
I am working on a betting API, The data comes from an XML Feed.
 
I am using a PHPPhar file to iterate the XML data, Seems to work really well. What I need to do now is iterate each "outcome" type and order it by the highest decimal to lowest decimal value (Best odds)
 
My current array of data from the XML looks like this (See pastebin for full version) :

SimpleXMLElement Object
(
    [Outcome] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 1
                            [name] => 1X2
                        )

                    [Bookmaker] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 8
                                            [name] => Bet365
                                            [isLive] => false
                                            [lastUpdate] => 2015-01-15T04:17:03
                                            [bookieEventID] => 
                                            [bookieLeagueID] => 
                                            [BookieOfferTypeID] => 40
                                        )

                                    [Odds] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 10026743121640944
                                                            [bet] => 1
                                                            [startPrice] => 9/2
                                                            [currentPrice] => 5/1
                                                            [line] => 
                                                            [LastUpdate] => 2015-01-15T04:17:03
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 10026743131640944
                                                            [bet] => 2
                                                            [startPrice] => 31/50
                                                            [currentPrice] => 57/100
                                                            [line] => 
                                                            [LastUpdate] => 2015-01-15T04:17:03
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [2] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 10026743511640944
                                                            [bet] => X
                                                            [startPrice] => 11/4
                                                            [currentPrice] => 3/1
                                                            [line] => 
                                                            [LastUpdate] => 2015-01-14T17:55:48
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                        )

                                )[1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 2
                            [name] => Under/Over
                        )

                    [Bookmaker] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 8
                                            [name] => Bet365
                                            [isLive] => false
                                            [lastUpdate] => 2015-01-15T15:25:31
                                            [bookieEventID] => 
                                            [bookieLeagueID] => 
                                            [BookieOfferTypeID] => 981
                                        )

                                    [Odds] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 14185280871640944
                                                            [bet] => Over
                                                            [startPrice] => 1/20
                                                            [currentPrice] => 3/50
                                                            [line] => 0.5
                                                            [LastUpdate] => 2015-01-15T15:15:14
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 8219239561640944
                                                            [bet] => Over
                                                            [startPrice] => 9/50
                                                            [currentPrice] => 1/5
                                                            [line] => 1.25
                                                            [LastUpdate] => 2015-01-15T08:30:43
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [2] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 13305582441640944
                                                            [bet] => Over
                                                            [startPrice] => 29/100
                                                            [currentPrice] => 8/25
                                                            [line] => 1.5
                                                            [LastUpdate] => 2015-01-15T15:25:31
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [3] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 15814388431640944
                                                            [bet] => Over
                                                            [startPrice] => 7/20
                                                            [currentPrice] => 2/5
                                                            [line] => 1.75
                                                            [LastUpdate] => 2015-01-15T08:30:43
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [4] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 9542253841640944
                                                            [bet] => Over
                                                            [startPrice] => 9/20
                                                            [currentPrice] => 13/25
                                                            [line] => 2.0
                                                            [LastUpdate] => 2015-01-15T08:53:07
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [5] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 18435608691640944
                                                            [bet] => Over
                                                            [startPrice] => 17/25
                                                            [currentPrice] => 77/100
                                                            [line] => 2.25
                                                            [LastUpdate] => 2015-01-15T08:53:07
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )[2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 17
                            [name] => Both Teams To Score
                        )

                    [Bookmaker] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 8
                                            [name] => Bet365
                                            [isLive] => false
                                            [lastUpdate] => 2015-01-14T17:55:48
                                            [bookieEventID] => 
                                            [bookieLeagueID] => 
                                            [BookieOfferTypeID] => 10150
                                        )

                                    [Odds] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 14294083431640944
                                                            [bet] => No
                                                            [startPrice] => 83/100
                                                            [currentPrice] => 73/100
                                                            [line] => 
                                                            [LastUpdate] => 2015-01-14T17:55:48
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 18070790851640944
                                                            [bet] => Yes
                                                            [startPrice] => 83/100
                                                            [currentPrice] => 1/1
                                                            [line] => 
                                                            [LastUpdate] => 2015-01-14T17:55:48
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                        )

                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [id] => 31
                                            [name] => William Hill
                                            [isLive] => false
                                            [lastUpdate] => 2015-01-15T15:58:30
                                            [bookieEventID] => 
                                            [bookieLeagueID] => 
                                            [BookieOfferTypeID] => 226344747
                                        )

                                    [Odds] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 11341591031640944
                                                            [bet] => No
                                                            [startPrice] => 73/100
                                                            [currentPrice] => 67/100
                                                            [line] => 
                                                            [LastUpdate] => 2015-01-15T15:58:30
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [id] => 10996680931640944
                                                            [bet] => Yes
                                                            [startPrice] => 1/1
                                                            [currentPrice] => 11/10
                                                            [line] => 
                                                            [LastUpdate] => 2015-01-15T15:58:30
                                                            [bookieOutcomeID] => 
                                                            [Status] => Open
                                                        )

                                                )

                                        )

                                )

Is it possible build an array of outcomes, Then sort by the best decimal price. (I have a function built that can convert the "currentPrice" to decimal.

 

Thnak You!

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/293959-create-an-array-based-on-array-key/
Share on other sites

If you had posted the original xml then I would look at it. I am not prepared to spend all evening editing print_r() output into something that can be processed.

 

Hi Barand,

 

Totally agree with your comment there and apologies! If I could have edited my original post I would have added a couple of things, Such as my actual PHP code.

 

An example of the XML feed is here

 

My current PHP code looks like :

foreach($array->Outcome as $outcome)
        {
            $outcometypes = $outcome->attributes();
            $outcomename = $outcometypes['name'];

            foreach($outcome->Bookmaker as $bookmaker)
            {
                $bookmakers = $bookmaker->attributes();
                $bookmaker_name = $bookmaker['name'];

                // Replace Spaces, William Hill Now = WilliamHill
                $bookmaker_name = str_replace( ' ', '', $bookmaker_name);

                foreach($bookmaker->Odds as $odds)
                {
                    $odds_types = $odds->attributes();
                    $currentprice = $odds_types['currentPrice'];
                    $decimalprice = $this->convert_decimal($currentprice);
                    $odds_types->addAttribute('decimal', $decimalprice);
                    

                    foreach($odds_types['bet'] as $bet)
                    {
                        
                    }
                }
            }
        }

Thank You!

I don't know the output you want but this should get you on your way

$xml = new SimpleXMLElement($_xml_string);

foreach ($xml->xpath("//Outcome") as $out) {
    $oid = $out['id'];
    foreach ($out->Bookmaker as $bk) {
        $bkname = $bk['name'];
        $bklu = $bk['lastUpdate'];
        foreach ($bk->Odds as $odd) {
            $outcomes[] = array (
                'Outcome'   => (string)$oid,
                'Bookmaker' => (string)$bkname,
                'Updated'   => (string)$bklu,
                'Bet'       => (string)$odd['bet'],
                'SP'        => (string)$odd['startPrice'],
                'CP'        => (string)$odd['currentPrice'],
                'Decimal'   => convertPrice ((string)$odd['currentPrice'])
            );
        }
    }
}

function convertPrice($price) {
    list ($a,$b) = explode('/', $price);
    return $a/($a+$b);
}

function priceSort($a, $b)
{
    return strcmp($a['Decimal'], $b['Decimal']) ;
}

usort($outcomes, 'priceSort');

echo '<pre>',print_r($outcomes, true),'</pre>';

Hi Barand,

 

Really appreciate your help on this one. Now I have the array in some form I need, I just need to split down based on market type to then do an sort on it. This has left me with something to go off. Many Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.