我有一个对象数组的例子,每次执行 foreach
循环时我都需要随机化它。
看起来 shuffle
只适用于数组。
一件事是我不能将它转换成一个数组,因为那样它将变成一个我不能使用的 STD 对象
,因为我的映射器的工作方式。
array(48) {
[0] => object(Friends_Model_Friends)#142 (4) {
["_id":protected] => NULL
["_tal":protected] => object(stdClass)#194 (3) {
["thumb"] => object(stdClass)#196 (6) {
["id"] => string(8) "10884697"
["type"] => string(1) "2"
["gallery"] => string(1) "1"
}
}
["_friend":protected] => string(7) "3492149"
["_dependent_table":protected] => NULL
}
[1] => object(Friends_Model_Friends)#195 (4) {
["_id":protected] => NULL
["_tal":protected] => object(stdClass)#143 (3) {
["thumb"] => object(stdClass)#202 (6) {
["id"] => string(8) "11160632"
["type"] => string(1) "2"
["gallery"] => string(1) "1"
}
}
["_friend":protected] => string(7) "3301541"
["_dependent_table":protected] => NULL
}
....
关于改组这个有什么想法吗?
编辑: Zend_Debug::dump(shuffle($object));
返回 bool(true)
最佳答案
<?php
$my_array;
echo '<pre>'; print_r($my_array); echo '</pre>';
shuffle ($my_array);
echo '<pre>'; print_r($my_array); echo '</pre>';
?>
试试这段代码
关于php - 如何随机化 PHP 中的对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11420387/