beanymanuk Posted July 9, 2013 Share Posted July 9, 2013 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 resultsI 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 More sharing options...
ayoksus Posted July 9, 2013 Share Posted July 9, 2013 simplexml is easy for you Link to comment https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/#findComment-1440028 Share on other sites More sharing options...
beanymanuk Posted July 9, 2013 Author Share Posted July 9, 2013 I've got it in simple xml but not sure about the filtering bit any helpI'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; }); Link to comment https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/#findComment-1440055 Share on other sites More sharing options...
AbraCadaver Posted July 9, 2013 Share Posted July 9, 2013 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); Link to comment https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/#findComment-1440064 Share on other sites More sharing options...
beanymanuk Posted July 10, 2013 Author Share Posted July 10, 2013 Thats not quite right not sorting correctly so not getting the highest 4 resultsArray 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 orderG5, G2, G1, G4 When result should beG1, G4, G3, G5 Link to comment https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/#findComment-1440239 Share on other sites More sharing options...
AbraCadaver Posted July 10, 2013 Share Posted July 10, 2013 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. Link to comment https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/#findComment-1440244 Share on other sites More sharing options...
beanymanuk Posted July 11, 2013 Author Share Posted July 11, 2013 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 Link to comment https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/#findComment-1440284 Share on other sites More sharing options...
beanymanuk Posted July 11, 2013 Author Share Posted July 11, 2013 Found the problem //wrong way round array_multisort($results, SORT_DESC, $amounts); //right way round array_multisort($amounts, SORT_DESC, $results); Link to comment https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/#findComment-1440290 Share on other sites More sharing options...
AbraCadaver Posted July 11, 2013 Share Posted July 11, 2013 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. Link to comment https://forums.phpfreaks.com/topic/279992-xml-into-array-filter-and-sort/#findComment-1440316 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.