문제

Been trying to figure this out for a while now, but can't seem to get it right. I'm working on an app that calls to an API and decodes a JSON response. I have the data, but I can not "work" with. Here is the data:

stdClass Object
(
    [custom_fields] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Heading 1
                    [0] => stdClass Object
                        (
                            [name] => sub_1-1
                            [caption] => SubHeading 1-1
                        )
                    [1] => stdClass Object
                        (
                            [name] => sub_1-2
                            [caption] => SubHeading 1-2
                        )
                )
            [1] => stdClass Object
                (
                    [name] => Heading 2 
                    [0] => stdClass Object
                        (
                            [name] => sub_2-1
                            [caption] => SubHeading 2-1
                        )
                )
        )
    [status] => 200
    [version] => 1.3.2
)

What I'm essentially trying to do is setup a table with Heading 1 then the 2 sub headings. I can get Heading 1 using $APIRESULT->custom_fields->name but I can not get the sub-headings. I'm sure it's easy and I'm just having a block, but I'm given up and hoping someone on here can help me real quick.

Thanks in advance.

도움이 되었습니까?

해결책

In PHP, variables cannot start with a digit, so you have to access it this way:

$APIRESULT->custom_fields->{'0'}->name // Access name

$APIRESULT->custom_fields->{'1'}->caption // Access something else

The solution should work, however I dislike the structure of that data as in my opinion arrays should be used instead of objects' properties to store some data such as several headings, etc

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top