【问题标题】:Help with array帮助数组
【发布时间】:2011-07-06 15:44:43
【问题描述】:

大家好,有问题

我有一个 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 从网络服务请求,但有时我收到此错误:

在php中不能使用字符串偏移作为数组

数组结果是:

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'];

有人可以帮忙解决一个如果通过错误?

肿块

【问题讨论】:

  • 我不知道如何解决您的问题。我只是想说——你这个可怜的灵魂。应该禁止这样的阵列。也许我们可以修改日内瓦公约,将其定义为酷刑?
  • 老实说,看起来 Web 服务部分的编码错误。我无法想象数组键“!msdata:IsDataSet”......看起来像一个函数调用。
  • 第二个数组是否是$result 的var_dump 出现错误?

标签: php arrays web-services fatal-error


【解决方案1】:

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

【讨论】:

  • 如何知道 $result['GetProductResult']['diffgram'] 是数组还是字符串?
  • is_array 似乎可以满足您的需求。
  • if( is_array($result['GetProductResult']['diffgram']) ) { ... }
  • $sub = $result['GetProductResult']['diffgram']; if($sub != ""){ $stock = $result['GetProductResult']['diffgram']['produtos']['produto']['stock']; echo "
    O produto $id_produto há $stock em stock

    ";这解决了我的问题谢谢:)
猜你喜欢
  • 2010-09-17
  • 2010-11-17
  • 2011-08-02
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多