帮助数组

时间:2011-07-06 15:44:43

标签: php arrays web-services fatal-error

你好,人们有问题,

我有一个由webservice返回的数组。

Array
(
[GetProductResult] => Array
    (
        [schema] => Array
            (
                [element] => Array
                    (
                        [complexType] => Array
                            (
                                [choice] => Array
                                    (
                                        [element] => Array
                                            (
                                                [complexType] => Array
                                                    (
                                                        [sequence] => Array
                                                            (
                                                                [element] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [!name] => codigo
                                                                                [!minOccurs] => 0
                                                                            )

                                                                        [1] => Array
                                                                            (
                                                                                [!name] => nome
                                                                                [!minOccurs] => 0
                                                                            )

                                                                        [2] => Array
                                                                            (
                                                                                [!name] => imagem
                                                                                [!minOccurs] => 0
                                                                            )

                                                                        [3] => Array
                                                                            (
                                                                                [!name] => stock
                                                                                [!minOccurs] => 0
                                                                            )

                                                                    )

                                                            )

                                                    )

                                                [!name] => produto
                                            )

                                        [!minOccurs] => 0
                                        [!maxOccurs] => unbounded
                                    )

                            )

                        [!name] => produtos
                        [!msdata:IsDataSet] => true
                        [!msdata:UseCurrentLocale] => true
                    )

                [!id] => produtos
            )

        [diffgram] => Array
            (
                [produtos] => Array
                    (
                        [produto] => Array
                            (
                                [codigo] => 13251
                                [nome] => Nova Development - Print Explosion Deluxe Mac
                                [imagem] => http://www.novadevelopment.com/images/3DBox_w250_tcm23-129399.jpg
                                [stock] => 0
                                [!diffgr:id] => produto1
                                [!msdata:rowOrder] => 0
                            )

                    )

            )

    )

我使用我的数据库中的id_produto从web服务请求,但有时我收到此错误:

  

不能在php

中使用字符串偏移量作为数组

AND ARRAY结果是:

Array
(
[GetProductResult] => Array
    (
        [schema] => Array
            (
                [element] => Array
                    (
                        [complexType] => Array
                            (
                                [choice] => Array
                                    (
                                        [element] => Array
                                            (
                                                [complexType] => Array
                                                    (
                                                        [sequence] => Array
                                                            (
                                                                [element] => Array
                                                                    (
                                                                        [0] => Array
                                                                            (
                                                                                [!name] => codigo
                                                                                [!minOccurs] => 0
                                                                            )

                                                                        [1] => Array
                                                                            (
                                                                                [!name] => nome
                                                                                [!minOccurs] => 0
                                                                            )

                                                                        [2] => Array
                                                                            (
                                                                                [!name] => imagem
                                                                                [!minOccurs] => 0
                                                                            )

                                                                        [3] => Array
                                                                            (
                                                                                [!name] => stock
                                                                                [!minOccurs] => 0
                                                                            )

                                                                    )

                                                            )

                                                    )

                                                [!name] => produto
                                            )

                                        [!minOccurs] => 0
                                        [!maxOccurs] => unbounded
                                    )

                            )

                        [!name] => produtos
                        [!msdata:IsDataSet] => true
                        [!msdata:UseCurrentLocale] => true
                    )

                [!id] => produtos
            )

        **[diffgram] =>**
    )

现在我如何避免这个错误?即时尝试访问数组:

$stock = $result['GetProductResult']['diffgram']['produtos']['produto']['stock'];

有人可以帮助一个If传递错误吗?

Cumps

1 个答案:

答案 0 :(得分:0)

该错误通常意味着您尝试作为数组访问的项目实际上是一个字符串。确保数组中的所有嵌套元素本身都是数组,而不是字符串。