在PHP中随机播放对象

str*_*ade 2 php shuffle object

如何在PHP中对对象进行排序?我试过shuffle()但是需要一个数组:

Warning: shuffle() expects parameter 1 to be array, 
object given in /var/www/index.php on line 366

Warning: Invalid argument supplied for foreach() in /var/www/index.php on line 334
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

public function updateStatusWithoutDB() {
    $this->updateProfileColors();
    $items = $this->getItems();
    $items = shuffle($items);
    if($this->updateStatusArray($items))
        return true;
    return false;
}
Run Code Online (Sandbox Code Playgroud)

A var_dump($items);返回此:

["180"]=>
    object(stdClass)#203 (1) {
      ["status"]=>
      string(130) "I was checking Microsoft's Visual Studio page just no…"
    }
Run Code Online (Sandbox Code Playgroud)

Dan*_*oap 12

您无法对对象进行排序,因为属性中没有顺序.

但是,您可以对对象的数组表示形式进行排序:

$arr = (array)$object;

shuffle($arr);
Run Code Online (Sandbox Code Playgroud)