1

Below is my XML file

<?xml version="1.0"?>
<calender>

<task>
<date>00/00/0000</date>
<title>My Birthday</title>
<description>Today is my birthday!</description>
</task>

<task>
<date>04/08/2013</date>
<title>test</title>
<description>swdefswde</description>
</task>

<task>
<date>04/02/2013</date>
<title>test</title>
<description>test</description>
</task>

<task>
<date>04/01/2013</date>
<title>egfwe</title>
<description>wefwef</description>
</task>

<task>
<date>04/03/2013</date>
<title>ssdv</title>
<description>ssdvs</description>
</task>

</calender>

I'm trying to add them into an array, and resort the elements by date [then rewrite the xml file with the sorted xml]. Can someone please help me?

I have tired the following code but doesnt work [cant even add them to array]

$xml_temp = array();
foreach ($xml_add->task as $aTask) {
    $xml_temp[] = $aTask;
    }

    print_r ($xml_temp);
1
  • What's wrong with $xml_temp? It looks fine. The next thing you need is usort. Commented Apr 9, 2013 at 3:15

2 Answers 2

3

Your array is fine. The next thing you need is usort:

$xml=simplexml_load_string(<<<XML
<?xml version="1.0"?>
<calender>

<task>
<date>00/00/0000</date>
<title>My Birthday</title>
<description>Today is my birthday!</description>
</task>

<task>
<date>04/08/2013</date>
<title>test</title>
<description>swdefswde</description>
</task>

<task>
<date>04/02/2013</date>
<title>test</title>
<description>test</description>
</task>

<task>
<date>04/01/2013</date>
<title>egfwe</title>
<description>wefwef</description>
</task>

<task>
<date>04/03/2013</date>
<title>ssdv</title>
<description>ssdvs</description>
</task>

</calender>
XML
);
$arr=array();
foreach($xml->task as $aTask)
{
    $arr[]=$aTask;
}
//print_r($arr);
/* uncomment the above line to debug */
usort($arr,function($a,$b){
    return strtotime($a->date)-strtotime($b->date);
});
//print_r($arr);
/* uncomment the above line to debug */
$xml=simplexml_load_string(<<<XML
<?xml version="1.0"?>
<calender>
</calender>
XML
);
foreach($arr as $aTask)
{
    $tTask=$xml->addChild($aTask->getName());
    $tTask->addChild($aTask->date->getName(),(string)$aTask->date);
    $tTask->addChild($aTask->title->getName(),(string)$aTask->title);
    $tTask->addChild($aTask->description->getName(),(string)$aTask->description);
}
echo $xml->asXML();

The echoed XML (manually formatting to make it look nicer):

<?xml version="1.0"?>
<calender>

<task>
<date>00/00/0000</date>
<title>My Birthday</title>
<description>Today is my birthday!</description>
</task>

<task>
<date>04/01/2013</date>
<title>egfwe</title>
<description>wefwef</description>
</task>

<task>
<date>04/02/2013</date>
<title>test</title>
<description>test</description>
</task>

<task>
<date>04/03/2013</date>
<title>ssdv</title>
<description>ssdvs</description>
</task>

<task>
<date>04/08/2013</date>
<title>test</title>
<description>swdefswde</description>
</task>

</calender>

Requires PHP >= 5.3

Live demo

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. You are a savior :)
Nice job! How is the sorted version output? In the foreach section, there are no echoes for output. Does the asXML() grab that too?
@buck1112 foreach was used to compose the whole XML (object), and then asXML() was used to generate a full XML (string).
0

You have multiple questions here:

  1. You want to sort a list of SimpleXMLElements. A reference question for that is:
  2. You need to append an element-into into a SimpleXMLElement which it does not support by default. This is outlined extensively in:

Additionally the question about how to search a list of SimpleXMLElements can be further subdivided so your concrete question is not clear. From your question it looks like you've already understood you need to convert the list of SimpleXMLElements into an array.

So you're probably looking for the reference question how to sort a multidimensional array as well:

As you can see your question has many facets which makes it hard to be answered. Stackoverflow works better if you ask a concrete programming question and not for a full script ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.