문제

PHP에서 객체를 어떻게 정렬 할 수 있습니까? 나는 시도했다 shuffle() 그러나 그것은 배열을 기대합니다.

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;
}

var_dump($items); 이것을 반환합니다 :

["180"]=>
    object(stdClass)#203 (1) {
      ["status"]=>
      string(130) "I was checking Microsoft's Visual Studio page just no…"
    }
도움이 되었습니까?

해결책

속성에 순서가 없으므로 객체를 정렬 할 수 없습니다.

그러나 객체의 배열 표현을 정렬 할 수 있습니다.

$arr = (array)$object;

shuffle($arr);

다른 팁

$ 항목을 배열로 사용하므로 $this->getItems() 배열 또는 사용을 반환합니다 get_object_vars($items) 객체의 배열을 얻으려면

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top