如果我知道搜索这些内容的正确术语,那么很容易通过谷歌搜索,但我不确定术语。
我有一个返回大对象的 API。我通过以下方式访问一个特定的:
$bug->fields->customfield_10205[0]->name;
//result is <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d272225232922280d2a202c2421632e2220" rel="noreferrer noopener nofollow">[email protected]</a>
有很多值,我可以通过将其从 0 更改为 1 等来访问它们
但我想循环遍历数组(也许这不是正确的术语)并获取其中的所有电子邮件并将其添加到如下字符串中:
implode(',', $array);
//This is private code so not worried too much about escaping
本以为我只是做类似的事情: echo implode(',', $bug->fields->customfield_10205->name);
也尝试过 echo implode(',', $bug->fields->customfield_10205);
还有 echo implode(',', $bug->fields->customfield_10205[]->name);
我正在寻找的输出是: '[email protected] , [email protected] , [email protected] '
我哪里错了,我提前为这个愚蠢的问题道歉,这可能是新手
最佳答案
您需要迭代,例如
# an array to store all the name attribute
$names = array();
foreach ($bug->fields->customfield_10205 as $idx=>$obj)
{
$names[] = $obj->name;
}
# then format it to whatever format your like
$str_names = implode(',', $names);
PS:您应该查找属性电子邮件而不是姓名,但是,我只是按照您的代码进行操作
关于php - 访问对象中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479923/