sort result pulled back via simplexml

Hi

I'm grabbing a list of payment cards back from part of a xml file like this:

$xml = simplexml_load_file('settings.xml.php');
echo $xml->cardsSet;

The trouble is the cards may be in any order and I want them sorted alphabetically, how can I acheive this please.

Full code for 500 points
LVL 3
Neil ThompsonSenior Systems DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RWJDComCommented:
Well.  You would probally want to put the contents of your XML data into an array of some sort and then you could just use the php function "sort".  It will sort the data however you want to sort it.

See my example below.  I can't write the full code without knowing what data is being received from your xml data but I think my attached code should help you out.
<?php
 
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}
 
?> 

Open in new window

0
cr4ck3rj4ckCommented:
Hi there,

As RWJDCom has said, can't be very specific due to not knowing much about the data.

The code below should be somewhere to start.

Hope that helps,
CJ
<?php
	$xml = simplexml_load_file("settings.xml.php");
 
	foreach($xml as $item)
	{
		$cardsSet[] = $xml->cardsSet;
	}
	sort($cardsSet);
 
	foreach($cardsSet as $item)
	{
		echo $item."</br>";
	}
?>

Open in new window

0
Neil ThompsonSenior Systems DeveloperAuthor Commented:
echo $xml->cardset would return variations on :

Switch,JCB,Visa Electron,Laser,MasterCard,Solo,Visa,Visa Delta

all comma seperated values
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Ray PaseurCommented:
Why not post a sample of the XML in the code snippet and tell us what fields you want it to be sorted on!
0
Neil ThompsonSenior Systems DeveloperAuthor Commented:
<?xml version="1.0"?>
<settings>
  <cardsAvailable>American Express,Solo,Diners Club,Switch,Visa,JCB,Laser</cardsAvailable>
  <cardsSet>Maestro,MasterCard,Visa Delta,GE Capital,Visa Electron</cardsSet>
</settings>

This can be in any order as It's added by the user. I want to show a list of accepted cards on the index page but would like them in A-Z order.

I use this to grab the line I want and simply write it:

$xml = simplexml_load_file('settings.xml.php');
echo $xml->cardsSet;

regards
Neil
0
Ray PaseurCommented:
Hi, Neil.  What fields do you want sorted?  cardsAvailable or cardsSet?  If the latter, try something like the code snippet.  HTH, ~Ray
<?php 
// SAMPLE XML STRING
$xml_string = '<?xml version="1.0"?>
<settings>
  <cardsAvailable>American Express,Solo,Diners Club,Switch,Visa,JCB,Laser</cardsAvailable>
  <cardsSet>Maestro,MasterCard,Visa Delta,GE Capital,Visa Electron</cardsSet>
</settings>
';
 
// GET SIMPLEXML
// WAS $xml = simplexml_load_file('settings.xml.php');
$xml - simplexml_load_string($xml_string);
 
// TURN CSV INTO ARRAY
$cardset_string = $xml->cardsSet
$cardset_array = explode(',', $cardset_string);
 
// SORT THE ARRAY
$cardset_array = sort($cardset_array);
 
// OPTIONAL - TURN ARRAY BACK INTO STRING
$cardset_string = implode(',', $cardset_array);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Neil ThompsonSenior Systems DeveloperAuthor Commented:
thanks, sorry for the dalay in wrapping points up!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.