如何将目标对象移动到索引数组对象的前面?

时间:2018-02-21 04:15:20

标签: php arrays sorting object multidimensional-array

我有一个索引的对象数组,如下所示:

Array
(
    [0] => St Object
        (
            [pId] => 6590153
            [prId] => 5371
            [expirationDate] => 2018-07-10 23:59:59           
            [creationDate] => 2018-01-10 11:58:29
            [pkid] => 12345
            [parentObj] => s Object
                (
                    [id] => 654250
                    [userID] => 776134
                    [pkid] ] => 12345
        )
     [ur] => 5899
         [overAgeRate] => 1.00
    )

    [1] => St Object
        (
            [pId] => 6590154
            [prId] => 5371
            [expirationDate] => 2018-07-10 23:59:59           
            [creationDate] => 2018-01-10 11:58:29
            [pkid] => 12346
            [parentObj] => s Object
                (
                    [id] => 654250
                    [userID] => 776134
                    [pkid] ] => 12346
        )
     [ur] => 58
         [overAgeRate] => 1.00
    )
)

我想根据我的针值重新排列数组。例如$needle=12346

基于此值,第二个对象需要成为第一个元素。结果数组将是:

Array
    (
        [0] => St Object
            (
                [pId] => 6590154
                [prId] => 5371
                [expirationDate] => 2018-07-10 23:59:59           
                [creationDate] => 2018-01-10 11:58:29
                [pkid] => 12346
                [parentObj] => s Object
                    (
                        [id] => 654250
                        [userID] => 776134
                        [pkid] ] => 12346
            )
         [ur] => 58
             [overAgeRate] => 1.00
        )

        [1] => St Object
            (
                [pId] => 6590153
                [prId] => 5371
                [expirationDate] => 2018-07-10 23:59:59           
                [creationDate] => 2018-01-10 11:58:29
                [pkid] => 12345
                [parentObj] => s Object
                    (
                        [id] => 654250
                        [userID] => 776134
                        [pkid] ] => 12345
            )
         [ur] => 5899
             [overAgeRate] => 1.00
        )
    )

我怎样才能以简单的方式做到这一点?

1 个答案:

答案 0 :(得分:1)

如果目标对象存在,这些方法会将目标对象移动到数组的前面,如果找不到目标,则会静默移动。

方法#1(Demo

$pkid='12346';
usort($array,function($a,$b)use($pkid){          // put needle into function scope
        if($a->pkid!=$pkid && $b->pkid!=$pkid){  // neither object matches needle
            return 0;                            // no move
        }
        return $a->pkid==$pkid ? -1 : 1;         // $a or $b matches; appropriately move
    });
var_export($array);

方法#2:(Demo

$pkid='12346';
foreach($array as $k=>$v){
    if($v->pkid== $pkid){                        // found the needle
        unset($array[$k]);                       // remove object from array
        array_unshift($array,$v);                // reinsert it at the front (reindex keys)
        break;                                   // no reason to continue iterating
    }
}
var_export($array);