如何在for循环中访问php中的数组中的对象?

时间:2015-07-27 15:52:46

标签: php arrays object

我以为会是这样的:

class  Object
    {
       public $ID;
       public $Name;

     }

我使用array_push($Objects, $Obj);

填充数组
for ( $d = 0; $d < count($Objects); $d++)
{
   echo $Objects[$d]->Name;
}

php给我一个错误说&#34;试图获取非对象的属性...&#34;

1 个答案:

答案 0 :(得分:0)

    class  Object
        {
           public $ID;
           public $Name = 'N';

         }

    $Objects =array();

    array_push($Objects,new Object());
    array_push($Objects,new Object());
    array_push($Objects,new Object());

    foreach($Objects as $obj){

        echo $obj->Name;
    }

    // NNN