Solved

Multi-dem array sort

Posted on 2006-07-20
7
275 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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

Industry Leaders: 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!

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

623 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