简体   繁体   English

将最后一个元素从stdclass对象数组的底部到顶部放置

[英]place the last element from bottom to top of stdclass object array

I have this array 我有这个数组

[questions] => Array
        (
            [0] => stdClass Object
                (
                    [quid] => 1
                )
            [1] => stdClass Object
                (
                    [quid] => 2
                )
            [2] => stdClass Object
                (
                    [quid] => 64
                )
        )

And I was trying to move the value 64 at the top and then 1 and 2 will come.My code is 我试图将值64移动到顶部,然后1和2就会出现。我的代码是

function sortById($x, $y) {
return $x->quid - $y->quid;
}

usort($_SESSION['questions'], 'sortById');

but it doesn't work. 但这不起作用。

A combination of array_unshift() and array_pop() will do the trick. 结合使用array_unshift()array_pop()即可达到目的。 This will always take the last element from the array and put it on the front. 这将始终从数组中获取最后一个元素,并将其放在最前面。

array_unshift($_SESSION['questions'], array_pop($_SESSION['questions']));

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM