Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Given this array:

Array
(
[0] => Array
    (
        [title] => this is the newest post
        [ssm_featured_post_id] => 70
    )

[1] => Array
    (
        [title] => sdfsfsdf
        [ssm_featured_post_id] => 63
    )

[2] => Array
    (
        [title] => test
        [ssm_featured_post_id] => 49
    )

[3] => Array
    (
        [title] => Hello world!
        [ssm_featured_post_id] => 1
    )

)

The ssm_featured_post_id value corresponds to the value of the array items in the second array. I want to order the first array items in the same order as the items in the second array

Array
(
    [1] => 63
    [0] => 70
    [3] => 1
    [2] => 49
)

so the result after sorting would be

Array
(
[0] => Array
    (

        [title] => sdfsfsdf
        [ssm_featured_post_id] => 63
    )

[1] => Array
    (
        [title] => this is the newest post
        [ssm_featured_post_id] => 70
    )

[2] => Array
    (
        [title] => Hello world!
        [ssm_featured_post_id] => 1

    )

[3] => Array
    (
        [title] => test
        [ssm_featured_post_id] => 49
    )

)
share|improve this question
    
Technically the second array isn't in the order you show in the output. The sequence that the values appear visually (63, 70, 1, 49) does not matter as the indices of the array (1, 0, 3, 2) are out of order. I expect this is merely a formatting error? –  JYelton Oct 30 '12 at 20:39
    
solution here stackoverflow.com/questions/8437076/… –  paul Oct 30 '12 at 20:43
    
I found that other thread which answered my question and when I came back I saw all these acceptable answers. what do I do? –  paul Oct 30 '12 at 20:47
    
Accept one. They both solve your problem. Pick whichever you think applies more to your problem. –  PWhite Oct 31 '12 at 0:26

2 Answers 2

up vote 1 down vote accepted

The simpler way would be to user usort and write a function that uses the second table to compare two values from first table.

share|improve this answer
    
That is a solution, whether it's "simpler" depends on the implementation. Valid answer though, voted. –  PWhite Oct 30 '12 at 20:41
    
Yeah maybe there is a simpler solution, but it seems to me that this would be the one I'd use –  Korri Oct 30 '12 at 20:45

You may want to check out array_multisort, particularly the third example given. The idea is that you create arrays based on the "columns" of the multidimensional array, then sort them simultaneously, and put the result back in the original array.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.