0

I am using the Amazon API to get some XML data, which is then passed through the json_decode() function.

Here are two samples of the data that is returned:

[BrowseNodes] => Array
    (
        [Request] => Array
            (
                [IsValid] => True
                [BrowseNodeLookupRequest] => Array
                    (
                        [BrowseNodeId] => 2645269011
                        [ResponseGroup] => BrowseNodeInfo
                    )

            )

        [BrowseNode] => Array
            (
                [BrowseNodeId] => 2645269011
                [Name] => Featured Categories
                [Children] => Array
                    (
                        [BrowseNode] => Array
                            (
                                [0] => Array
                                    (
                                        [BrowseNodeId] => 3741261
                                        [Name] => Cooktops
                                    )

                                [1] => Array
                                    (
                                        [BrowseNodeId] => 3741271
                                        [Name] => Dishwashers
                                    )

                                [2] => Array
                                    (
                                        [BrowseNodeId] => 3741331
                                        [Name] => Freezers
                                    )

                                [3] => Array
                                    (
                                        [BrowseNodeId] => 2399939011
                                        [Name] => Ice Makers
                                    )

                            )

                    )

and

[BrowseNodes] => Array
    (
        [Request] => Array
            (
                [IsValid] => True
                [BrowseNodeLookupRequest] => Array
                    (
                        [BrowseNodeId] => 3774781
                        [ResponseGroup] => BrowseNodeInfo
                    )

            )

        [BrowseNode] => Array
            (
                [BrowseNodeId] => 3774781
                [Name] => Vitamin D
                [Children] => Array
                    (
                        [BrowseNode] => Array
                            (
                                [BrowseNodeId] => 6936848011
                                [Name] => D3
                            )

                    )

I am then using this code to obtain data for each of the children:

if(isset($result['BrowseNodes']['BrowseNode']['Children'])){
$childs = $result['BrowseNodes']['BrowseNode']['Children']['BrowseNode'];
        foreach($childs as $child){
            $browsenodeid = $child['BrowseNodeId'];
            $name = $child['Name'];
        }
    }

Everything works fine in the first sample, but in the second one, I am getting the following errors:

Warning: Illegal string offset 'BrowseNodeId'

Warning: Illegal string offset 'Name'

If I echo $browsenodeid and $name in the second example, it gives me 6 and D as the output.

Any ideas as to what I am doing wrong? I am quite confused; to me the data I am looping through in both cases seems to be the same, the only difference being that in the first case, the array has 4 elements and in the second it has 1 element.

Thanks in advance.

badcoder
  • 3,624
  • 5
  • 32
  • 33

1 Answers1

0

The reason I think is that in case 1: the structure is [BrowseNode][0][BrowseNodeId] whereas in case 2 it is : [BrowseNode][BrowseNodeId] that [0] is missing , you can fix it in the data.

Read more at: Illegal string offset Warning PHP

Community
  • 1
  • 1
frunkad
  • 2,433
  • 1
  • 23
  • 35