Jump to content

Recommended Posts

Hi this is what I want to do

 

Read values from xml into two dimensional array and then filter out ones with currency GBP and then sort by amount DESC and output the top 4 results

I can get xml into php ok but I don't know how to loop those results into an array and then do the rest.

Any help would be muchly appreciated

 

Thanks

 

EG of XML

<accumulators>
<accumulator>
<game_id>Test</game_id>
<currency>EUR</currency>
<game>Test</game>
<game_type>midlet</game_type>
<amount>22269</amount>
<display_amount>€222.69</display_amount>
</accumulator>

<accumulator>
<game_id>Test2</game_id>
<currency>GBP</currency>
<game>PharaohsFortunesSlots</game>
<game_type>midlet</game_type>
<amount>6348</amount>
<display_amount>£63.48</display_amount>
</accumulator>
</accumulators>
Link to comment
https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/
Share on other sites

I've got it in simple xml but not sure about the filtering bit any help

I've tried this so far

$xml = array_filter($xml, function($obj){
    if (isset($obj->accumulator)) {
        foreach ($obj->accumulator as $accumulators ) {
            if ($accumulators->currency != 'GBP') return false;
        }
    }
    return true;
});

This should work:

foreach($xml->accumulator as $accumulator) {
	if($accumulator->currency != 'GBP') {
		$results[] = (array)$accumulator;
		$amounts[] = (array)$accumulator->amount;
	}
}
array_multisort($results, SORT_DESC, $amounts);

$top4 = array_slice($results, 0, 4);

Thats not quite right not sorting correctly so not getting the highest 4 results

Array before sort

Array ( [0] => Array ( [game_id] => G1 [currency] => GBP [game] => G1 [game_type] => midlet [amount] => 111830 [display_amount] => £1,118.30 ) [1] => Array ( [game_id] => G2 [currency] => GBP [game] => G2 [game_type] => midlet [amount] => 23831 [display_amount] => £238.31 ) [2] => Array ( [game_id] => G3 [currency] => GBP [game] => G3 [game_type] => midlet [amount] => 38963 [display_amount] => £389.63 ) [3] => Array ( [game_id] => G4 [currency] => GBP [game] => G4 [game_type] => midlet [amount] => 86691 [display_amount] => £866.91 ) [4] => Array ( [game_id] => G5 [currency] => GBP [game] => G5 [game_type] => midlet [amount] => 25000 [display_amount] => £250.00 ) ) 

I am getting result in order
G5, G2, G1, G4

 

When result should be
G1, G4, G3, G5

My code would not return any of those because you stated "filter out the ones with currency GBP", so I took that to mean remove.  My code will not include anything where currency=GBP.  It sorted correctly as I wrote it.  If you changed it to include GBP then the changes have broken the sort.

 

If you state what you want more clearly and post all XML or a link to it I'll see what works.

Edited by AbraCadaver

Hi

 

Code
 

<?php
$xml=simplexml_load_file("http://www.peter-gosling.com/testxml.xml");
foreach($xml->accumulator as $accumulator) {
	if(($accumulator->currency == 'GBP') && (($accumulator->game_id == 'PF') || ($accumulator->game_id == 'ST') || ($accumulator->game_id == 'TO2') || ($accumulator->game_id == 'LO2') || ($accumulator->game_id == 'KQ'))) {
		$results[] = (array)$accumulator;
		$amounts[] = (array)$accumulator->amount;
	}
}
//print_r($results);
array_multisort($results, SORT_DESC, $amounts);

$top4 = array_slice($results, 0, 4);

print_r($top4);
?>

So I want Top 4 games out of the 5 games I'm pulling out based on the amount

My bad, good catch.  Also, since I'm here I would probably simplify the IF and make it more extendable:

$currencies = array('GBP'); //can add more
$game_ids = array('PF', 'ST', 'TO2', 'LO2', 'KQ');

//etc...

if(in_array($accumulator->currency, $currencies) && in_array_($accumulator->game_id, $game_ids)){

This would also be easy to add functionality later wherein someone uses a form to select the currency and/or game_ids.

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.