?
Solved

sort result pulled back via simplexml

Posted on 2008-11-10
7
Medium Priority
?
510 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:Neil Thompson
7 Comments
 
LVL 8

Expert Comment

by:RWJDCom
ID: 22923268
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
 
LVL 6

Expert Comment

by:cr4ck3rj4ck
ID: 22923292
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
 
LVL 3

Author Comment

by:Neil Thompson
ID: 22923343
echo $xml->cardset would return variations on :

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

all comma seperated values
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 22926693
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
 
LVL 3

Author Comment

by:Neil Thompson
ID: 22928526
<?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
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 22938905
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
 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 31515103
thanks, sorry for the dalay in wrapping points up!
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question