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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/758449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:46:32  来源:igfitidea点击:

How do I access this object property with a hyphenated name?

phpobject

提问by Ian

I'm using a PHP class someone wrote to interface with the BaseCamp API.

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

The particular call I'm doing is to retrieve the items in a todo list, which works fine.

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

My problem is, I'm not sure how to access just the todo-itemsproperty of the object that is returned. Here's the var_dump of the returned object:

我的问题是,我不确定如何仅访问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)

How can I access the todo-itemsportion of this object?

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

回答by Alister Bulman

<?php
$x = new StdClass();
$x->{'todo-list'} = 'fred';
var_dump($x);

So, $object->{'todo-list'} is the sub-object. If you can set it like that, then you can also read it the same way.

所以, $object->{'todo-list'} 是子对象。如果你可以这样设置,那么你也可以用同样的方式阅读它。

If you wanted to convert it to an array, which can be a little more easily (ie the obvious $ret['todo-list'] accessing), this code is taken almost verbatim from Zend_Config and will convert for you.

如果你想把它转换成一个更容易一点的数组(即明显的 $ret['todo-list'] 访问),这段代码几乎是从 Zend_Config 中逐字提取的,并将为你转换。

public function toArray()
{
    $array = array();
    foreach ($this->_data as $key => $value) {
        if ($value instanceof StdClass) {
            $array[$key] = $value->toArray();
        } else {
            $array[$key] = $value;
        }
    }
    return $array;
}

回答by Nikunj Dhimar

Try this simplest way!

试试这个最简单的方法!

$obj = $myobject->{'mydash-value'};
$objToArray = array($obj);