多维数组,寻找项目,并移动到顶部? [英] Multidimensional array, find item and move to the top?

查看:96
本文介绍了多维数组,寻找项目,并移动到顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些样的功能,将发现(以下阵列中)与2 ID的对象,并将其移动到数组的顶部。这里的原始数组:

 阵列

    [0] => stdClass的对象
        (
            [ID] => 177
            [STARTDATE] => 2014年8月2日
        )    [1] => stdClass的对象
        (
            [ID] => 178
            [STARTDATE] => 2014年8月2日
        )    [2] => stdClass的对象
        (
            [ID] => 2
            [STARTDATE] => 2014年7月28日
        )    [3] => stdClass的对象
        (
            [ID] => 82
            [STARTDATE] => 2014年7月28日        )    [4] => stdClass的对象
        (
            [ID] => 199
            [STARTDATE] => 2013年10月10日
        )

这里是什么我想它想输出(与移动数组项):

 阵列
(    [0] => stdClass的对象
        (
            [ID] => 2
            [STARTDATE] => 2014年7月28日
        )
    [1] => stdClass的对象
        (
            [ID] => 177
            [STARTDATE] => 2014年8月2日
        )    [2] => stdClass的对象
        (
            [ID] => 178
            [STARTDATE] => 2014年8月2日
        )    [3] => stdClass的对象
        (
            [ID] => 82
            [STARTDATE] => 2014年7月28日        )    [4] => stdClass的对象
        (
            [ID] => 199
            [STARTDATE] => 2013年10月10日
        )

任何帮助将是AP preciated。


解决方案

 函数customShift($数组的$ id){
    的foreach($数组$关键=> $ VAL){//循环的所有元素
        如果($ val-> ID ==的$ id){//检查ID的$ id
            未设置($数组[$关键]); //取消设置$阵列ID的$ id
            array_unshift($数组$ VAL); //不印字带$ VAL阵推在数组的开头
            返回$阵列; //返回新的数组$
        }
    }
}的print_r(customShift($数据,2));

I'm trying to do some kind of function that will find (in the following array) the object with the id of 2, and move it to the top of the array. Here's the original array:

Array
(
    [0] => stdClass Object
        (
            [id] => 177
            [startdate] => 2014-08-02
        )

    [1] => stdClass Object
        (
            [id] => 178
            [startdate] => 2014-08-02
        )

    [2] => stdClass Object
        (
            [id] => 2
            [startdate] => 2014-07-28
        )

    [3] => stdClass Object
        (
            [id] => 82
            [startdate] => 2014-07-28

        )

    [4] => stdClass Object
        (
            [id] => 199
            [startdate] => 2013-10-10
        )
)

And here is what I'd like it to output (with the moved array item):

Array
(

    [0] => stdClass Object
        (
            [id] => 2
            [startdate] => 2014-07-28
        )
    [1] => stdClass Object
        (
            [id] => 177
            [startdate] => 2014-08-02
        )

    [2] => stdClass Object
        (
            [id] => 178
            [startdate] => 2014-08-02
        )

    [3] => stdClass Object
        (
            [id] => 82
            [startdate] => 2014-07-28

        )

    [4] => stdClass Object
        (
            [id] => 199
            [startdate] => 2013-10-10
        )
)

Any help would be appreciated.

解决方案

function customShift($array, $id){
    foreach($array as $key => $val){     // loop all elements
        if($val->id == $id){             // check for id $id
            unset($array[$key]);         // unset the $array with id $id
            array_unshift($array, $val); // unshift the array with $val to push in the beginning of array
            return $array;               // return new $array
        }
    }
}

print_r(customShift($data, 2));

这篇关于多维数组,寻找项目,并移动到顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆