duannai1883 2014-08-03 02:39
浏览 163
已采纳

在foreach循环php中更改对象的属性

I have an array of objects $comments.
I get it through calling TableGateway wise call to database.

$comments = $this->getCommentsTable()->getComments($theme_id);

I now need to change one of the properties of array object elements:

    foreach ($comments as $comment) {
        $comment->liked = 1;
    }

But the value of $comment->liked doesn't change.

I now need to add a new property disliked:

    foreach ($comments as $comment) {
        $comment->disliked = 1;
    }

But it doesn't add (I know it by the following error):

( ! ) Notice: Undefined property: My\Model\Comments::$disliked  in C:\my\project\module\my\view\Name\operation\comment-info-panel.phtml on line 31
Call Stack
#   Time    Memory  Function    Location
1   0.0008  255368  {main}( )   ..\index.php:0
2   0.1599  6312880 Zend\Mvc\Application->run( )    ..\index.php:26
3   1.8049  9176328 Zend\Mvc\Application->completeRequest( )    ..\Application.php:322
4   1.8049  9176520 Zend\EventManager\EventManager->trigger( )  ..\Application.php:347
5   1.8050  9176648 Zend\EventManager\EventManager->triggerListeners( )

I tried the following with reference:

    foreach ($comments as &$comment) {
        $comment->disliked = 1;
    }

But I get a nasty error:

( ! ) Fatal error: An iterator cannot be used with foreach by reference in C:\My\Project\module\Name\src\Name\Controller\MyController.php on line 804
Call Stack
#   Time    Memory  Function    Location
1   0.0010  255368  {main}( )   ..\index.php:0
2   0.1601  6312880 Zend\Mvc\Application->run( )    ..\index.php:26

Thanks to find a way to modify and/or add a property on an object in this array.

  • 写回答

1条回答 默认 最新

  • douqihou7537 2014-08-04 01:07
    关注

    I did it in the following way:

        $commArray = array(); // Create a new array
        $count = $comments->count(); // Get the count of the resultset rows
    
        // Loop over the count
        for ($i = 0; $i < $count; $i++) {
    
            // Get the current object
            $curComm = $comments->current();
    
            // Here I can change easily a property of row object
            $curComm->liked = 1;
    
            // Here I can easily add a new property to row object
            $curComm->disliked = 1;
    
            // Set the cursor of the resultset to the next row object
            $comments->next();
    
            // Add the current row object to the array
            array_push($commArray, $curComm);
        }
    
        // Do whatever you like with your new array of resultset
        return $commArray;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?