致命错误:不能将 UserEntityUser 类型的对象用作数组

问题是:

<?php foreach ($users as $user): ?>
<?php $user = $user[0] ?>

错误:

=> Fatal error: Cannot use object of type UserEntityUser as array in ../module/User/view/user/index/index.phtml

我的课是:

class IndexController extends ApplicationController
{
/**
 * Store the user.
 * @var UserEntityUser
 */
protected $_user;

public function indexAction()
{
    $offset = 6;
    $page = $this->params()->fromQuery('page');
    if (!$page) {
        $page = 1;
    }

    $dql = "SELECT
            users
         FROM UserEntityUser users

        GROUP BY users.id
        ORDER BY " . $this->getOrderDql();

    $query = $this->objectManager()
        ->createQuery($dql)
        ->setFirstResult(($page - 1) * $offset)
        ->setMaxResults($offset);

    $users = new Paginator($query, $fetchJoinCollection = true);
    $quantity = (int) ceil($users->count() / $offset);

    return new ViewModel(
        array(
            'users' => $users,
            'offset' => $offset,
            'page' => $page,
            'quantity' => $quantity,
            'order' => $this->getOrderValue(),
        )
    );
}

}

感谢您的回答!

(使我的帖子主要不是代码的文字)

stack overflow Fatal error: Cannot use object of type User\Entity\User as array
原文答案

答案:

作者头像

正如错误所说,您 cant 将对象用作数组,

您需要按以下方式访问对象,

$user->'keyname';$user->'filedname' 而不是 $user[0]

其中 keyname 或 field name 是您要检索的字段的名称。