views:

318

answers:

2

how can i sort object in php? i tryed 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

here is the code

  public function updateStatusWithoutDB()
  {
    $this->updateProfileColors();
    $items = $this->getItems();
    $items = shuffle($items);
    if($this->updateStatusArray($items))
      return true;
    return false;  

  }

an var_dump($items); return this

  ["180"]=>
  object(stdClass)#203 (1) {
    ["status"]=>
    string(130) "I was checking Microsoft's Visual Studio page just no…"
  }
+1  A: 

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);
Cassy
would be posible to acces objects the? like $obj->myvar ?
streetparade
if you casted the $arr back to an object, then yes. $obj = (object) $arr; would work.
RibaldEddie
thanks this worked for me have a nice evening
streetparade
A: 

Since you are using $items as an array, either make $this->getItems() return an array or use get_object_vars($items) to get array of object's vars.

StasM