gpt4 book ai didi

php - 如何使用非法名称访问此对象属性?

转载 作者:IT老高 更新时间:2023-10-28 11:49:03 24 4
gpt4 key购买 nike

我正在使用某人编写的 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 部分?

最佳答案

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

所以,$object->{'todo-list'} 是子对象。如果你可以这样设置,那么你也可以这样读:

echo $x->{'todo-list'};

另一种可能性:

$todolist = 'todo-list';
echo $x->$todolist;

如果你想将它转换为一个数组,这可能更容易一点(即明显的 $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;
}

关于php - 如何使用非法名称访问此对象属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/758449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com