3

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
    )

)
4
  • 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? Commented Oct 30, 2012 at 20:39
  • solution here stackoverflow.com/questions/8437076/… Commented Oct 30, 2012 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? Commented Oct 30, 2012 at 20:47
  • Accept one. They both solve your problem. Pick whichever you think applies more to your problem. Commented Oct 31, 2012 at 0:26

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

That is a solution, whether it's "simpler" depends on the implementation. Valid answer though, voted.
Yeah maybe there is a simpler solution, but it seems to me that this would be the one I'd use
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.