Solved

Multi-dem array sort

Posted on 2006-07-20
7
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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