简体   繁体   English

如何访问数组中的对象元素

[英]How to access object elements in an array

I'm working with the Github PHP Library . 我正在使用Github PHP库 There is a specific call that you can make that uses the Buzz HTTP Client as shown by the following: 您可以使用Buzz HTTP Client进行特定调用,如下所示:

$client->getHttpClient()->get('repos/:user/:repo/events');

The problem is the return of that request is something that I'm unsure how to access the elements: 问题是返回该请求是我不确定如何访问元素的事情:

Github\HttpClient\Message\Response Object
(
    [remainingCalls] => 
    [protocolVersion:Buzz\Message\Response:private] => 0
    [statusCode:Buzz\Message\Response:private] => 200
    [reasonPhrase:Buzz\Message\Response:private] => OK
    [headers:Buzz\Message\AbstractMessage:private] => 

    [content:Buzz\Message\AbstractMessage:private] => 
)

Now I can loop through the object doing something like the following: 现在我可以循环执行对象,执行以下操作:

foreach( $events as $item ) {
   print_r( $item );
}

But I really only care about the content inside of headers and content . 但我真的只关心headerscontent

Is anyone aware of how I can directly access those elements directly without the need to loop? 是否有人知道如何直接访问这些元素而无需循环?

If you look at the source for that object class you'll see that there's a getContent() function that will retrieve the value of the content field. 如果查看该对象类的源代码,您将看到有一个getContent()函数将检索内容字段的值。 You will also notice that this class extends the Response class from the Buzz Client. 您还会注意到此类从Buzz客户端扩展了Response类 The Buzz Response class in turn is an extension of the AbstractMessage class which has a getHeaders() function as well as a getContent() function. 反过来,Buzz Response类是AbstractMessage类的扩展,它具有getHeaders()函数和getContent()函数。

So essentially you can access those two variables using the getContent() and getHeaders() functions provided through inheritance. 所以基本上你可以使用通过继承提供的getContent()和getHeaders()函数来访问这两个变量。

Have you tried this? 你试过这个吗?

$res = $client->getHttpClient()->get('repos/:user/:repo/events');
echo $res->remainingCalls;  // non private var

or you should be able to call every public method of the Response Class 或者您应该能够调用Response类的每个公共方法

$res->getStatusCode();
$res->getProtocolVersion();

Inspect the possible methods here: 检查可能的方法:

https://github.com/kriswallsmith/Buzz/blob/master/lib/Buzz/Message/Response.php https://github.com/kriswallsmith/Buzz/blob/master/lib/Buzz/Message/Response.php

https://github.com/kriswallsmith/Buzz/blob/master/lib/Buzz/Message/AbstractMessage.php https://github.com/kriswallsmith/Buzz/blob/master/lib/Buzz/Message/AbstractMessage.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM