如何在foreach循环中将变量字符串与数组一起使用

时间:2019-05-30 08:23:56

标签: php xml loops

我尝试使以下代码正常工作。

foreach ($uniqueItems as $key => $value) {
 $output = "{$value->properties->property[10]->value}";
 echo $output;
}

在浏览器中,我看到24.99

以上代码为我提供了正确的输出。但是我需要这个foreach之外的$输出。我尝试以下

$output = "{\$value->properties->property[10]->value}";
foreach ($uniqueItems as $key => $value) {
 echo $output;
}

在浏览器中,我看到{$ value-> properties-> property [10]-> value}

这是print_r($ uniqueItems [$ key]);

SimpleXMLElement Object
(
    [name] => Donnay joggingbroek zwart unisex
    [properties] => SimpleXMLElement Object
        (
            [property] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => deliveryTime
                                )

                            [value] => Voor 16.00 uur besteld, morgen in huis!
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => brand
                                )

                            [value] => Donnay
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => size
                                )

                            [value] => SimpleXMLElement Object
                                (
                                )

                        )

                    [3] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => color
                                )

                            [value] => Zwart
                        )

                    [4] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => EAN
                                )

                            [value] => SimpleXMLElement Object
                                (
                                )

                        )

                    [5] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => categoryPath
                                )

                            [value] => Tenniskleding/Tenniskleding dames
                        )

                    [6] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => deliveryCosts
                                )

                            [value] => 4.95
                        )

                    [7] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => discount
                                )

                            [value] => 5.00
                        )

                    [8] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => subcategories
                                )

                            [value] => Tenniskleding dames
                        )

                    [9] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => SKU
                                )

                            [value] => 489000-TL-020
                        )

                    [10] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [name] => fromPrice
                                )

                            [value] => 24.99
                        )
))

如何获得正确的输出?

2 个答案:

答案 0 :(得分:0)

您可以将其与外部变量连接,如下所示:-

$output = '';

foreach ($uniqueItems as $key => $value) {
    $output .= "{$value->properties->property[10]->value}"."\r\n"; //\r\n for new line
}
echo $output;

答案 1 :(得分:0)

第二个代码段将不起作用,因为php不知道要对$value进行插值,直到定义了$value,而这实际上是在循环真正开始时发生的。到那时,它只是一个字符串。这就是为什么它只是将其打印为字符串