Solved

Multi-dem array sort

Posted on 2006-07-20
7
269 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:qwertq
Comment Utility
why is 1 coming after a then?
0
 
LVL 7

Expert Comment

by:maUru
Comment Utility
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
Comment Utility
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
Comment Utility
Try passing the constant SORT_STRING as a second parameter to ksort.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now