Forum Moderators: coopster
I have an array ($myarray) that contains details about pages on a site.
Each item in $myarray relates to a page on the site.
Each item / page is represented by a standard set of details stored in an array (let's call it $mypage).
For example:
$mypage always contains [url,title,date_added_to_site,sub_heading].
So a typical $myarray looks like:
[
['index.php','home','20060511','my home page'],
['about.php','about us','20060512','all you need to know about us'],
['sitemap.php','site map','20060512','our sitemap']
]
I am having a real struggle understanding the manual as to how I sort this by (say) "date_added_to_site" using a standard function.
Am I correct in saying that sort($myarray) is equivalent to sorting the pages into ascending url (sorting on the '0' item in each $mypage)?
Can I sort on (say) $mypage[2] with a standard function and if so what (as I read it array_multisort does not do this - I don't want to sort $mypage, I just want to sort $myarray on a common key within $mypage).
Many thanks, Sam.
PS. Should I be using a different array structure?
What I did in the end was to array_unshift my sort ket on the front of each of my "rows" and then do a standard sort.
Thus each row became (using my example)
[sortkey,url,title,date_added_to_site,sub_heading] and I simply sorted the resulting array of arrays. I then accessed item[n+1] of each sub-array instead of item[n] when pulling out data. (In other words I didn't bother to remove the sort key after the sort - though you could of course).
$new_array = array();
$this_item = current($old_array);
while (current($old_array)):
// add the required sort key to the front of the item
array_unshift($this_item, $this_item[$the_key]);
// put the result in the new array (at the end, but where doesn't matter)
array_push($new_array, $this_item);
next($old_array);
$this_item = current($old_array);
endwhile;
// sort the new version - the required key being on the front
sort($new_array);
Thanks for the ideas.