3

给定这个数组:

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
    )

)

ssm_featured_post_id 值对应于第二个数组中数组项的值。我想以与第二个数组中的项目相同的顺序对第一个数组项进行排序

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

所以排序后的结果是

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

2 回答 2

2

更简单的方法是使用usort并编写一个函数,该函数使用第二个表来比较第一个表中的两个值。

于 2012-10-30T20:40:25.157 回答
1

您可能想查看array_multisort,特别是给出的第三个示例。这个想法是您根据多维数组的“列”创建数组,然后同时对它们进行排序,并将结果放回原始数组中。

于 2012-10-30T20:38:03.140 回答