IT

하이픈으로 이름이 지정된이 개체 속성에 어떻게 액세스 할 수 있습니까?

lottoking 2020. 8. 5. 07:39
반응형

하이픈으로 이름이 지정된이 개체 속성에 어떻게 액세스 할 수 있습니까?


BaseCamp API와 인터페이스하기 위해 누군가 PHP 클래스를 사용하고 있습니다.

내가하고있는 특정 전화는 할 일 목록에서 항목을 검색하는 것입니다.

내 문제 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이 개체 의 일부 어떻게 액세스 할 수 있습니까?


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

따라서 $ object-> { 'todo-list'}가 하위 오브젝트입니다. 그렇게 설정하면 같은 방식으로 읽을 수도 있습니다.

좀 더 쉽게 할 수있는 배열로 변환하려는 경우 (예 : 명백한 $ 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;
}

이 가장 간단한 방법을 시도하십시오!

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

참고 URL : https://stackoverflow.com/questions/758449/how-do-i-access-this-object-property-with-a-hyphenated-name

반응형