Forum Moderators: coopster

Message Too Old, No Replies

[how] sorting text node

PHP-simpexml

         

mochan

9:24 am on Apr 9, 2009 (gmt 0)

10+ Year Member



hello, i have some script, that im trying to sort the value of userid from small to big number..but until now im only stuck to print the value of userid like structure in $string

and this is my script
<?
$string = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<values>
<user>
<userid>21</userid>
</user>
<user>
<userid>48</userid>
</user>
<user>
<userid>11</userid>
</user>
<user>
<userid>56</userid>
</user>
</values>
XML;

$xml = simplexml_load_string($string);
foreach($xml->children() as $children){
echo $children->userid."<br/>";
}
?>

So does anyone have any ideas on this one?
Thanks :)

andrewsmd

1:58 pm on Apr 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how about this

$string = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<values>
<user>
<userid>21</userid>
</user>
<user>
<userid>48</userid>
</user>
<user>
<userid>11</userid>
</user>
<user>
<userid>56</userid>
</user>
</values>
XML;

$xml = simplexml_load_string($string);

$sort = array();

foreach($xml->children() as $children){

//store the integer value of the id in the array
array_push($sort, (intval($children->userid)));

}//foreach

//sort the array
asort($sort);

//show the ids
foreach($sort as $i){

echo("$i<br>");

}//foreach

mochan

7:13 am on Apr 10, 2009 (gmt 0)

10+ Year Member



thanx its really help me..but i have new problem, if i have some xml structure like this

<values>
<user>
<userid>21</userid>
<name>chandra</name>
<age>20</age>
</user>
<user>
<userid>48</userid>
<name>bara</name>
<age>23</age>
</user>
<user>
<userid>11</userid>
<name>taqin</name>
<age>21</age>
</user>
<user>
<userid>56</userid>
<name>abin</name>
<age>22</age>
</user>
</values>

then i want to print name and age nodes based on`userid from small to big number..can u help me please..im really weak bout array

andrewsmd

7:32 pm on Apr 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about this then

<html><?php
$string = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<values>
<user>
<userid>21</userid>
<name>chandra</name>
<age>20</age>
</user>
<user>
<userid>48</userid>
<name>bara</name>
<age>23</age>
</user>
<user>
<userid>11</userid>
<name>taqin</name>
<age>21</age>
</user>
<user>
<userid>56</userid>
<name>abin</name>
<age>22</age>
</user>
</values>
XML;

$xml = simplexml_load_string($string);

$output = array();

foreach($xml->children() as $children){

//change the key to an int
$key = intval($children->userid);

//change the name to a string
$name = strval($children->name);

//change the age to a string
$age = strval($children->age);

//store the key of the array
//of the other input into an array inside of that
$output[$key] = array("Name" => $name, "Age" => $age);

}//foreach

//sort the array based on the ids
ksort($output);

echo("<table>");
//now print out the informaiton
foreach($output as $i){

//echo a new row
echo("<tr>");

foreach($i as $key => $j){

echo("<td>$key:</td><td>$j</td>");

}//foreach j

//close the row
echo("</tr>");
}//foreach
?> </html>