通过变量访问PHP对象数组

时间:2019-04-26 14:39:40

标签: php arrays

我有一个示例对象数组作为变量$ xmlobj

SimpleXMLElement Object(
[SearchId] => 10769920113727_1556288871357_170040
[Categories] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [total_subcategories] => 1
            )

        [SubCategory] => SimpleXMLElement Object
            (
                [Title] => Reproductor MP3 y Multimedia
                [Value] => 122701
                [NumberOfProducts] => 1
            )

    )

[Products] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [totalResultsAvailable] => 1
                [firstResultPosition] => 1
                [totalResultsReturned] => 1
                [currency] => EUR
                [totalMerchantsAvailable] => 1
                [searchOperator] => and
            )

        [Product] => SimpleXMLElement Object
            (
                [Offer] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [id] => f94af9ec5186b53051c9ccf083ebddec
                                [type] => MANUFACTURER_PRODUCTS
                            )

                        [Title] => Apple iPod Nano 6 8 GB Gris
                        [Description] => iPod Nano 6 8 GB - Gris
                        [LastModified] => 2019-04-26 01:10:56
                        [MobileFriendly] => false
                        [Merchant] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [id] => 15348713
                                    )

                                [Name] => Backmarket

                            )

                        [Price] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [currency] => EUR
                                    )

                                [Price] => 99.99
                                [DeliveryCost] => 19.9
                                [DeliveryCostDetails] => SimpleXMLElement Object
                                    (
                                        [DeliveryCost] => 19.9
                                    )

                                [TotalPrice] => 119.89
                                [PriceWithoutRebate] => 190
                                [Rebate] => 47
                            )

                        [ProductClass] => 2
                        [Availability] => 1
                        [DeliveryTime] => 24H / 4 días laborables
                        [Promo] => SimpleXMLElement Object
                            (
                            )

                        [OffensiveContent] => false
                        [FinancingOption] => SimpleXMLElement Object
                            (
                            )

                        [MerchantCategory] => Alta Tecnología  Imágenes y sonidos  Reproductor de MP3 Y MP4
                        [Brand] => Apple
                        [BrandId] => 211
                        [GreenProduct] => false
                    )

            )

    )

现在,当我想访问例如价格我是这样的:

$product_total_price = $xmlobj->Products->Product->Offer->Price->TotalPrice;

没关系-我得到了想要的东西,但是当我想“动态地”改变想要的东西时遇到了问题:

$search_part = "Products->Product->Offer->Price->TotalPrice";
$product_total_price = $xmlobj->$search_part;

即使我尝试,我显然也什么也没得到……

$product_total_price = $xmlobj."->".$search_part;

所以我的问题是...该怎么做? :)

谢谢!

2 个答案:

答案 0 :(得分:2)

您将必须通过展开为数组,然后循环获取每个属性来构造路径。

例如...

$searchPart = "Products->Product->Offer->Price->TotalPrice";
$searchPath = explode('->', $searchPart);

$current = $xmlobj;
foreach ($searchPath as $segment) {
    $current = $current->$segment;
}
var_dump($current);

或者您可以将其转换为数组,然后就可以获取属性

$xml = simplexml_load_string($xmlobj);
$json = json_encode($xml);
$arr = json_decode($json,TRUE);

$parseString = 'Products->Product->Offer->Price->@attributes->currency';
$parsePath = explode('->', $parseString);

$current = $arr;
foreach ($parsePath as $segment) {
    $current = $current[$segment];
}
var_dump($current);

答案 1 :(得分:0)

您可以查看将代码作为字符串并对其进行评估的enter image description here,但是我强烈不建议使用它,因为这是一个潜在的(而且非常大的)安全漏洞。

相反,您可以使用eval作为第一个参数->传递,然后遍历结果数组以获取所需的值:

$search_part = "Products->Product->Offer->Price->TotalPrice";
$chunks = explode("->", $search_part);

foreach ($chunks as $chunk) {
  $temp = $xmlobj[$chunk];
}

$product_total_price = $temp;

或类似的东西,然后您将动态获得价格

请注意,如果$xmlObj的类型为Object,则需要使用$xmlObj->{$chunk}而不是$xmlobj[$chunk]