PHP foreach无法正常工作

时间:2015-01-26 14:20:47

标签: php foreach

我的XML看起来像这样:

                  [image] => SimpleXMLElement Object
                            (
                                [foto_0] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339643.JPG
                                [foto_1] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339933.JPG
                                [foto_2] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339384.JPG
                                [foto_3] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339853.JPG
                                [foto_4] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339334.JPG
                                [foto_5] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339229.JPG
                                [foto_6] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339487.JPG
                                [foto_7] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339570.JPG
                                [foto_8] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339096.JPG
                                [foto_9] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339516.JPG
                                [foto_10] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339911.JPG
                                [foto_11] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339201.JPG
                                [foto_12] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339042.JPG
                                [foto_13] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339307.JPG
                                [foto_14] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339754.JPG
                                [foto_15] => http://pics.auto-commerce.eu/ac1358/foto/1421058132339305.JPG
                            )

现在我希望每张图片都插入到数据库中。保存代码工作正常。但是foreach不起作用。谁想帮助我?

Foreach代码:

foreach($car->image as $photourl){ *DB code* }

谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个:

foreach($car->image->children() as $photourl){
    echo "Key: " . $photourl->getName() . "<br>";
    echo "Value: " . trim((string)$photourl) . "<br>";
}