按指定ID排序数组?

时间:2014-11-09 14:37:22

标签: php sorting

我有以下数组,指示如何对DATA数组进行排序的顺序:

 $PARENT_ID_ORDER = array(1, 3, 2); 

这是DATA数组,其中包含PARENT_ID键,我想使用PARENT_ID_ORDER按PARENT_ID键排序:

 $DATA = array (
     array (
        'PARENT_ID' => 2;
     ),
     array (
        'PARENT_ID' => 2;
     ),
     array (
        'PARENT_ID' => 1;
     ),
     array (
        'PARENT_ID' => 3;
     ),
     array (
        'PARENT_ID' => 1;
     ),
     array (
        'PARENT_ID' => 2;
     ),
     array (
        'PARENT_ID' => 2;
     )
 );

预期产出:

 array(
        [0] => Element Object
            (
                [PARENT_ID] => 1,
            ),
        [1] => Element Object
            (
                [PARENT_ID] => 1,
            ),
        [2] => Element Object
            (
                [PARENT_ID] => 3,
            ),
        [3] => Element Object
            (
                [PARENT_ID] => 2,
            ),
        [4] => Element Object
            (
                [PARENT_ID] => 2,
            ),
        [5] => Element Object
            (
                [PARENT_ID] => 2,
            ),
        [6] => Element Object
            (
                [PARENT_ID] => 2,
            )
 );

如何将我的订单数组提供给排序函数,以便按此方式排序?谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找http://php.net/manual/en/function.usort.php。 如果数组很小,您可以将它与二进制搜索或内置array_search一起使用