解析具有许多元素和对象的php数组

时间:2018-08-31 14:11:21

标签: php arrays parsing

我是php数组的新手,并努力使自己了解如何解析它们中的数据。我看过How can I access an array/object?

但我无法控制。需要一个例子来理解。所以我在$ result变量中有这个数组

 This topic focuses on three key default fields:

        host
        source
        sourcetype

    Defining host, source, and sourcetype

    The host, source, and sourcetype fields are defined as follows:

        host - An event host value is typically the hostname, IP address, or fully qualified domain name of the network host from which the event originated. The host value lets you locate data originating from a specific device. For more information on hosts, see About hosts.
        sourcetype - The source type of an event is the format of the data input from which it originates, such as access_combined or cisco_syslog. The source type determines how your data is to be formatted. For more information on source types, see Why source types matter.

    Source vs sourcetype

    Source and source type are both default fields, but they are entirely different otherwise, and can be easily confused.

        The source is the name of the file, stream, or other input from which a particular event originates. 

        The sourcetype determines how Splunk software processes the incoming data stream into individual events according to the nature of the data.

    Events with the same source type can come from different sources, for example, if you monitor source=/var/log/messages and receive direct syslog input from udp:514. If you search sourcetype=linux_syslog, events from both of those sources are returned. 

Git Hub -

Logback configuration looks like: 

```xml 
<!-- Splunk HTTP Appender --> 
<appender name="splunkHttpAppender" class="com.splunk.logging.HttpEventCollectorLogbackAppender"> 
<url>${lsplunk.http.url}</url> 
<token>${splunk.http.token}</token> 
<source>${splunk.source}</source> 
<host>${splunk.httpevent.listener.host}</host> 
<messageFormat>${splunk.event.message.format}</messageFormat> 
<disableCertificateValidation>${splunk.cert.disable-validation}</disableCertificateValidation> 
<layout class="ch.qos.logback.classic.PatternLayout"> 
<pattern>%date{ISO8601} [%thread] %level: %msg%n</pattern> 
</layout> 
</appender> 

<logger name="com.example.app" additivity="false" level="INFO"> 
<appender-ref ref="splunkHttpAppender"/> 
</logger> 

<root level="INFO"> 
<appender-ref ref="splunkHttpAppender"/> 
</root>

如何获取[CURRENT_PRIVILEGESET]的值?我尝试了以下内容,但我认为我误会了一些东西。感谢大家的耐心等待,而我尝试着解决这个问题。

Array
(
    [response] => Array
        (
            [data] => Array
                (
                    [0] => Array
                        (
                            [fieldData] => Array
                                (
                                    [CURRENT_PRIVILEGESET] => FM Data API
                                )
                            [portalData] => Array
                                (
                                )
                            [recordId] => 1
                            [modId] => 0
                        )
                    [1] => Array
                        (
                            [fieldData] => Array
                                (
                                    [CURRENT_PRIVILEGESET] => FM Data API
                                )
                            [portalData] => Array
                                (
                                )
                            [recordId] => 2
                            [modId] => 0
                        )
                )
        )
    [messages] => Array
        (
            [0] => Array
                (
                    [code] => 0
                    [message] => OK
                )
        )
)

3 个答案:

答案 0 :(得分:2)

$result['response']['data'][0]['fieldData']['CURRENT_PRIVILEGESET'];

您已经使用->的对象表示法,但是整个过程都在使用数组,而不是对象。

答案 1 :(得分:1)

您的示例中没有对象。使用->是指对象。这些都是数组,因此您只需按以下键即可:

$privilege = $result["response"]['data'][0]["fieldData"]["CURRENT_PRIVILEGESET"];

答案 2 :(得分:0)

无论何时要遍历(移动)对象,都可以使用箭头({->)表示法来完成,而对于数组,则可以通过访问索引({[x])表示法来实现,其中{ {1}}可以是x,例如int,也可以是[1],例如string

我假设您知道什么是类,对象以及数组。 假设我们有['data']的{​​{1}}和class类的Test的{​​{1}},无论何时我们object,从上到下,您会看到这样的内容:

Test

这仅表示我们正在进行var_dumping的$testObj属于var_dump($testobj)类,我们可以像这样访问object(Test)[1] public 'feeling' => string 'Love' (length=4) public 'inverse' => string 'hate' (length=4) 的{​​{1}}属性:     object;

Test的结构并不大不同。如果我们有一个名为feeling的{​​{1}}并进行了$testObj,我们将看到:

$testObj->feeling

同样,我们看到类型被声明;在这种情况下,arrays。我们可以像这样访问一些索引:     $ testArr ['feeling'] //输出爱     $ testArr [3] //输出300

对于您的用例,您可以像这样遍历到array

$testArr

查看这些资源以获取更多信息。 http://php.net/manual/en/language.types.object.php http://php.net/manual/en/language.types.array.php