doucai1901 2015-10-11 19:11
浏览 63

循环数组时出现PHP“非法字符串偏移”错误

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.

  • 写回答

1条回答 默认 最新

  • dseve40868 2015-10-11 19:18
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大