Forum Moderators: coopster

Message Too Old, No Replies

Sorting an array of standard arrays

         

fish_eye

1:30 am on May 10, 2006 (gmt 0)

10+ Year Member



Sort-newbie question....

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?

eelixduppy

1:43 am on May 10, 2006 (gmt 0)



Hello..

I think this may point you in the right direction
[us3.php.net...]

good luck!

eelix

fish_eye

2:32 am on May 10, 2006 (gmt 0)

10+ Year Member



I think this may point you in the right direction

Do you mean I sort each $mypage (using ksort) to have the desired key at the front and then sort $myarray (using sort)? And by using ksort the first item in each $mypage will still be [2] (using my example).

fish_eye

5:52 am on May 10, 2006 (gmt 0)

10+ Year Member



Do you mean I sort each $mypage (using ksort)

Silly me - of course not. Either I misunderstand what you're saying or vice versa. Please explain.

adb64

10:25 am on May 10, 2006 (gmt 0)

10+ Year Member



You may better use uksort [us3.php.net] for which you can specify your own compare function, the result of that function will determine how your array will be sorted

jatar_k

2:55 pm on May 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



there is also
[php.net...]

fish_eye

5:10 am on May 11, 2006 (gmt 0)

10+ Year Member



I'd looked at array_multisort but it seemed to me from the examples that it allowed me to sort each of my "rows" within themselves (rather than all rows based on a common "column"). Maybe I should look again next time.

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.