php - 如何随机化 PHP 中的对象数组?

标签 php arrays object random shuffle

我有一个对象数组的例子,每次执行 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/

相关文章:

Javascript:将数字变量从函数传递给setInterval

java repaint() 和初始化对象

c# - 在 C# 中将 JsonResult 转换为不同的对象

php - 在哪里保存数据?

php - 正则表达式 - 排除子域(preg_match)

php - 使用发布数据进行数组搜索获取数组中的下一个元素

c++ - 在非成员函数中访问MFC对话框的成员变量

php - 显示帖子在 WordPress 上更新的最后日期

java - 在Android中,我如何按日期对数组项进行分组?

javascript - 将 2D 数组的 2D 网格展平为单个 2D 数组,在 JavaScript 中(功能上?)