Domanda

Come posso ordinare un oggetto in PHP? Ho provato shuffle() ma che prevede un 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

Questo è il mio codice:

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

Un var_dump($items); restituisce questo:

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

Soluzione

Non è possibile ordinare un oggetto, dal momento che non esiste un ordine negli attributi.

Tuttavia, è possibile ordinare una rappresentazione di matrice di un oggetto:

$arr = (array)$object;

shuffle($arr);

Altri suggerimenti

Dal momento che si sta utilizzando $ articoli come un array, o fare $this->getItems() restituire un array o utilizzare get_object_vars($items) per ottenere serie di Vars dell'oggetto.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top