PHP 中的随机对象 [英] Shuffle objects in PHP
本文介绍了PHP 中的随机对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 PHP 中对对象进行排序?我试过 shuffle()
但它需要一个数组:
How can I sort an object in PHP? I tried shuffle()
but that expects an array:
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
这是我的代码:
public function updateStatusWithoutDB() {
$this->updateProfileColors();
$items = $this->getItems();
$items = shuffle($items);
if($this->updateStatusArray($items))
return true;
return false;
}
A var_dump($items);
返回:
["180"]=>
object(stdClass)#203 (1) {
["status"]=>
string(130) "I was checking Microsoft's Visual Studio page just no…"
}
推荐答案
您不能对对象进行排序,因为属性中没有顺序.
You cannot sort an object, since there is no order in the attributes.
但是,您可以对对象的数组表示进行排序:
However, you can sort an array representation of an object:
$arr = (array)$object;
shuffle($arr);
这篇关于PHP 中的随机对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文