Forum Moderators: coopster
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 :)
$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
<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
<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>