数组foreach循环中的“警告:尚无法将复杂类型分配给属性”

时间:2018-11-12 12:56:19

标签: php arrays simplexml

我有这个SimpleXMLElement对象。

 [prices] => SimpleXMLElement Object
        (
            [price] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [term] => 0.5
                            [price] => 35
                            [currency] => EUR
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [term] => 1
                            [price] => 60
                            [currency] => EUR
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [term] => 2
                            [price] => 50
                            [currency] => EUR
                        )
                )
         )
)

尝试创建$term[]数组...

foreach($xmlData->prices->price as $term) {
            $term[] = array(
            'term' => (string)$term->term,
            'price' => (string)$term->price,
            'currency' => (string)$term->currency
            );
        }

但是得到警告:尚无法为以下字符串中的属性分配复杂类型$term[] = array(

如何解决这个问题?

通过将$term[] = array(更改为$terms[] = array(

来解决

0 个答案:

没有答案