Solved

Multi-dem array sort

Posted on 2006-07-20
7
272 Views
Last Modified: 2008-03-17
Multi-demension arrays always get me... I need help sorting this array two ways.

Array
(
    [1] => Array
        (
            [timestamp] => 1153408578
        )
    [2] => Array
        (
            [timestamp] => 1153408589
        )
    [3] => Array
        (
            [timestamp] => 1153408592
        )
    [2.txt] => Array
        (
            [timestamp] => 1153408593
        )
    [3.txt] => Array
        (
            [timestamp] => 1153408593
        )
)

#1 : I would like to sort the array by the key, maintaining the inside array. I tried ksort, but it did not appear to work.

#2 : I would like to sort the array the array but the timestamp, maintaining the associations.

Thanks
0
Comment
Question by:qwertq
  • 3
  • 2
  • 2
7 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 250 total points
ID: 17148618
Can you show what you tried for #1, what it produced, and what you wanted instead?

For #2, we'll extract the timestamps in order, and then pass that as the first parameter to array_multisort, and your multi-d array as the second..
$reference = array();
foreach ($yourArray as $row)
  $reference[] = $row['timestamp'];
array_multisort($reference, $yourArray);
0
 

Author Comment

by:qwertq
ID: 17149553
this array has been ksort($array) 'ed

notice how it is out of order (the keys). the 1 is at the bottom and the 1.txt is at the top... ?

Array
(
    [1.txt] => Array
        (
            [timestamp] => 1153408592
        )

    [2.txt] => Array
        (
            [timestamp] => 1153408593
        )

    [3.txt] => Array
        (
            [timestamp] => 1153408593
        )

    [a] => Array
        (
            [timestamp] => 1153408596
        )

    [a.jpg] => Array
        (
            [timestamp] => 1153414940
        )

    [b] => Array
        (
            [timestamp] => 1153408599
        )

    [b.jpg] => Array
        (
            [timestamp] => 1153417257
        )

    [c] => Array
        (
            [timestamp] => 1153408604
        )

    [c.jpg] => Array
        (
            [timestamp] => 1153417279
        )

    [1] => Array
        (
            [timestamp] => 1153408578
        )

    [2] => Array
        (
            [timestamp] => 1153408589
        )

    [3] => Array
        (
            [timestamp] => 1153408592
        )

)
0
 
LVL 7

Expert Comment

by:maUru
ID: 17149663
that is normal ksort behaviour, 1.txt comes before 1

and 1. comes before 1.txt

no idea why, something do with how ksort works..
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:qwertq
ID: 17149780
why is 1 coming after a then?
0
 
LVL 7

Expert Comment

by:maUru
ID: 17149825
why are we here?
is there a god?
why dont you ask the guy who wrote ksort.

it seems a string 'a' will come before a string '1'

but an integer 1 will come before a string 'a'

0
 

Author Comment

by:qwertq
ID: 17149932
can you suggest an atlerntaive to sort by key as all strings. i would like it to display the normal human way of alphabatizing
0
 
LVL 33

Assisted Solution

by:snoyes_jw
snoyes_jw earned 250 total points
ID: 17153853
Try passing the constant SORT_STRING as a second parameter to ksort.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question