如何使用带连字符的名称访问此对象属性?

如何使用带连字符的名称访问此对象属性?

我正在使用某人编写的PHP类来与BaseCamp API进行交互。

我正在做的特定调用是检索待办事项列表中的项目,这很好。

我的问题是,我不知道如何只访问todo-items返回的对象的属性。这是返回对象的var_dump:

object(stdClass)[6]
  public 'completed-count' => string '0' (length=1)
  public 'description' => string 'Description String' (length=89)
  public 'id' => string '12345' (length=7)
  public 'milestone-id' => string '' (length=0)
  public 'name' => string 'Error Reports' (length=13)
  public 'position' => string '1' (length=1)
  public 'private' => string 'false' (length=5)
  public 'project-id' => string '58904' (length=7)
  public 'tracked' => string 'false' (length=5)
  public 'uncompleted-count' => string '1' (length=1)
  public 'todo-items' => 
    object(stdClass)[3]
      public 'todo-item' => 
        object(stdClass)[5]
          public 'completed' => string 'false' (length=5)
          public 'content' => string 'content string here' (length=133)
          public 'created-on' => string '2009-04-16T20:33:31Z' (length=20)
          public 'creator-id' => string '23423' (length=7)
          public 'id' => string '234' (length=8)
          public 'position' => string '1' (length=1)
          public 'responsible-party-id' => string '2844499' (length=7)
          public 'responsible-party-type' => string 'Person' (length=6)
          public 'todo-list-id' => string '234234' (length=7)
  public 'complete' => string 'false' (length=5)

如何访问todo-items此对象的部分?


慕的地8271018
浏览 349回答 2
2回答

慕桂英3389331

<?php $x&nbsp;=&nbsp;new&nbsp;StdClass();$x->{'todo-list'}&nbsp;=&nbsp;'fred';var_dump($x);所以,$ object - > {'todo-list'}是子对象。如果您可以这样设置,那么您也可以以相同的方式阅读它。如果你想将它转换为一个数组,这可能会更容易(即明显的$ ret ['todo-list']访问),这个代码几乎是从Zend_Config逐字逐句获取并将为你转换。public&nbsp;function&nbsp;toArray(){ &nbsp;&nbsp;&nbsp;&nbsp;$array&nbsp;=&nbsp;array(); &nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;($this->_data&nbsp;as&nbsp;$key&nbsp;=>&nbsp;$value)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($value&nbsp;instanceof&nbsp;StdClass)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$array[$key]&nbsp;=&nbsp;$value->toArray(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$array[$key]&nbsp;=&nbsp;$value; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$array;}
打开App,查看更多内容
随时随地看视频慕课网APP