在特定元素上重新排列数组

时间:2012-05-28 07:12:52

标签: php arrays

我需要将数组项的顺序更改为预定义顺序。

原始数组是

$order = array(3136,3121,3147,3125);

Array
(
    [3121] => Array
        (
            [0] => 311
            [1] => 311>
            [2] => First Level
        )

    [3125] => Array
        (
            [0] => 312
            [1] => 312>
            [2] => Ids
        )

    [3136] => Array
        (
            [0] => 313
            [1] => 313>
            [2] => Insurance
        )

    [3147] => Array
        (
            [0] => 314
            [1] => 314>
            [2] => Property
        )
)

需要生成如下所示,

Array
(
    [3136] => Array
        (
            [0] => 313
            [1] => 313>
            [2] => Insurance
        )
    [3121] => Array
        (
            [0] => 311
            [1] => 311>
            [2] => First Level
        )
    [3147] => Array
        (
            [0] => 314
            [1] => 314>
            [2] => Property
        )
    [3125] => Array
        (
            [0] => 312
            [1] => 312>
            [2] => Ids
        )

)

Base将是数组元素的关键,3136元素3121将被推后3147等等

1 个答案:

答案 0 :(得分:2)

只是一个猜测:

$order = array(3136,3121,3147);
$new_arr =array();
foreach ($order as $key){
    if ($old_array[$key])
        $new_arr[$key]=$old_array[$key];
}