Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组,查找项目并移到顶部?_Php_Arrays_Multidimensional Array - Fatal编程技术网

Php 多维数组,查找项目并移到顶部?

Php 多维数组,查找项目并移到顶部?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我正在尝试执行某种函数,以查找(在下面的数组中)id为2的对象,并将其移动到数组的顶部。以下是原始阵列: Array ( [0] => stdClass Object ( [id] => 177 [startdate] => 2014-08-02 ) [1] => stdClass Object ( [id] => 178

我正在尝试执行某种函数,以查找(在下面的数组中)id为2的对象,并将其移动到数组的顶部。以下是原始阵列:

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
        )
)
下面是我希望它输出的内容(使用移动的数组项):


任何帮助都将不胜感激。

我刚刚意识到数组键也是2,我不希望它移动它,我需要它移动[id]=>2的一个。不要在评论中编辑问题,请在问题中进行编辑。您已经尝试过什么?比较(当不需要查看内部数组时):在找到记录或修复代码时从foreach中断,$out数组可以有多行,多行可以取消移位。我同意ID应该是唯一的,但您永远不知道人们在尝试做什么,这就是我提出或提议的原因;-)p、 您可以将代码缩小2行,删除$out变量,并在设置$out的位置取消移位。您认为我们可以将其缩小吗?;-)只是开玩笑+1.
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));
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));