PHP SimpleXML Array_multisort() Kind Of Works

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 …

via PHP Website Development » Search Results » ajax:

PHP SimpleXML Array_multisort() Kind Of Works

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:

http://www.moorelife.org/rss/SavedToServe.xml
Sun, 06 Nov 2011 11:00:00 CST

http://www.moorelife.org/rss/FilledWithAllTheFullnessOfGod.xml
Mon, 03 Oct 2011 19:00:00 CST
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 “

n";
print_r($items);
print "

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
………………………………………

You could directly sort the simplexml objects first:
$nodes = array(
new SimpleXMLElement(‘

http://www.moorelife.org/rss/SavedToServe.xml
Sun, 06 Nov 2011 11:00:00 CST
‘),
new SimpleXMLElement(‘

http://www.moorelife.org/rss/FilledWithAllTheFullnessOfGod.xml
Mon, 03 Oct 2011 19:00:00 CST
‘)
);

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);

For more info: PHP SimpleXML Array_multisort() Kind Of Works

PHP Website Development » Search Results » ajax

PHP SimpleXML Array_multisort() Kind Of Works

Share this post:

Related Posts

Leave a Comment