Sorting a 2-dimensional array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Sorting a 2-dimensional array

Post by HiddenS3crets »

I have an array that follows this structure

Code: Select all

Array (
     [category1]
     (
          [0]
          (
               [title] = A Note
               [timestamp] = 012097521
          )
          [1]
          (
               [title] = Another Note
               [timestamp] = 120974325
          )
     )
     [category2]
     (
          [0]
          (
               [title] = A Third Note
               [timestamp] = 093297534
          )
          [1]
          (
               [title] = A Fourth Note
               [timestamp] = 012190785
          )
     )
)
What would be the easiest way to sort this array by timestamp and store it in a new array?

I have a way to do this but it sorts with respect to the category, i.e., it will sort each array separately by timestamp. I need a way to sort it with no regard to what category it falls under

An example of the outputted, sorted array would look like this:

Code: Select all

Array
(
     [category2]
     (
          [title] = A Fourth Note
          [timestamp] = 182190785
     )
     [category1]
     (
          [title] = Another Note
          [timestamp] = 120974325
     )
     [category2]
     (
          [title] = A Third Note
          [timestamp] = 093297534
     )
     [category1]
     (
          [title] = A Note
          [timestamp] = 088097521
     )
)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Sorting a 2-dimensional array

Post by Christopher »

I would copy the data into an array something like the below, and then sort by timestamp:

Code: Select all

Array (
          [0]
          (
               [title] = A Note
               [timestamp] = 012097521
               [category] = category1
          )
          [1]
          (
               [title] = Another Note
               [timestamp] = 120974325
               [category] = category1
          )
          [2]
          (
               [title] = A Third Note
               [timestamp] = 093297534
               [category] = category2
          )
          [3]
          (
               [title] = A Fourth Note
               [timestamp] = 012190785
               [category] = category2
          )
)
(#10850)
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Re: Sorting a 2-dimensional array

Post by HiddenS3crets »

ok I'll go with that, I wasn't sure if there was some function that I was overlooking that could make the process more simple
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sorting a 2-dimensional array

Post by Jonah Bron »

The reason you can't do that is because you cannot have multiple indices in an array with the same key.
Post Reply