0

I have a multidimensional array (standard RSS fare - channel - item - title) and I'm trying to sort it by title. I'm using SimpleXML and created an array of the items from the SimpleXML objects. Then I made another array of titles from the items array. Then I sorted the items array by title using array_multisort().

It is kind of working. There are 122 items. The sort puts 2 items in order, then 80 items in order, then 38 items in order, then 2 items in order. I'm not sure why it's splitting them up into 4 groups, though.

XML:

<channel>
    <item>
        <title>Saved To Serve</title>
        <guid>http://www.moorelife.org/rss/SavedToServe.xml</guid>
        <pubDate>Sun, 06 Nov 2011 11:00:00 CST</pubDate>
    </item>
    <item>
        <title>Filled With All The Fullness Of God</title>
        <guid>http://www.moorelife.org/rss/FilledWithAllTheFullnessOfGod.xml</guid>
        <pubDate>Mon, 03 Oct 2011 19:00:00 CST</pubDate>
    </item>
</channel>

PHP:

<?php
$items = array();
foreach($data->channel->item as $item) {
    $items[] = $item;
}
$titles = array();
foreach($items as $item) {
    $titles[] = $item["title"];
}
array_multisort($titles, SORT_ASC, $items);
print "<pre>\n";
print_r($items);
print "</pre>\n";
?>

In the xml file, Saved To Serve is right before Filled With All The Fullness Of God (just like the sample). When sorted, FWATFOG should be 50 or so items before STS. In the actual output, STS is #2 and FWATFOG is #86 (in the 3rd alphabetical grouping).

My ultimate goal is to be able to have links for sorting by title or date (preferably with AJAX so we don't have to deal with page reloadings). Do you guys have any ideas or suggestions?

JJ

1
  • Apparently, there is something else at work here. I tried doing SORT_DESC as well as creating other arrays ($pubDates[] = $item["pubDate"];) and everything outputs in the same order. The only time it outputs differently is if I put a wrong value in and it sorts the way the xml is.
    – doubleJ
    Dec 2, 2011 at 17:10

1 Answer 1

0

You could directly sort the simplexml objects first:

$nodes = array(
    new SimpleXMLElement('<item>
        <title>Saved To Serve</title>
        <guid>http://www.moorelife.org/rss/SavedToServe.xml</guid>
        <pubDate>Sun, 06 Nov 2011 11:00:00 CST</pubDate>
    </item>
'),
    new SimpleXMLElement('<item>
        <title>Filled With All The Fullness Of God</title>
        <guid>http://www.moorelife.org/rss/FilledWithAllTheFullnessOfGod.xml</guid>
        <pubDate>Mon, 03 Oct 2011 19:00:00 CST</pubDate>
    </item>
')
);

function xsort(&$nodes, $child_name, $order = SORT_ASC)
{
    $sort_proxy = array();

    foreach ($nodes as $k => $node)
    {
        $sort_proxy[$k] = (string) $node->$child_name;
    }

    array_multisort($sort_proxy, $order, $nodes);
}

xsort($nodes, 'title', SORT_ASC);

print_r($nodes);
1
  • That didn't work for me (blank page). It may be worth mentioning that I'm using php4 using the simplexml for php4 class (phpclasses.org/package/…). I didn't think to mention it, as it hasn't been a problem. It seems to work with the objects in the same way. I know it doesn't support xpath or wildcards, though, so it may not be working with $node->$child_name or something.
    – doubleJ
    Dec 2, 2011 at 15:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.