سؤال

كيف يمكنني فرز كائن في 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) للحصول على مجموعة من vars الكائن.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top