php array_filter 问题,试图取出数据,但它不起作用

问题描述 投票:0回答:1

我对 PHP 还很陌生,我正在尝试找出 array_filter,以及为什么它似乎不适合我。 我从 namecheap 沙箱中得到了一个大数组,可以向我的一位同事进行测试和学习。

因此,我将数组和指定的域 TLD 传递到我的函数中,并且我期望从“注册”产品类别返回单个价格。

class registerPrice {
    function getRegisterPrice($domainTLD, $newArr) {
        // Extract the "Register" product category for the target TLD
        $registerCategory = array_filter($newArr['CommandResponse']['UserGetPricingResult']['ProductType'], function ($category) use ($domainTLD) {
            return $category['@attributes']['Name'] === 'register' && $category['Product']['@attributes']['Name'] === $domainTLD;
        });

        // Extract the prices for the 1-year duration from the "register" category for the target TLD
        $registerPrices = array_column($registerCategory, 'Product');
        $registerPriceForOneYear = null;
        foreach ($registerPrices as $product) {
            foreach ($product as $price) {
                if ($price['@attributes']['Duration'] == 1 && $price['@attributes']['DurationType'] == 'YEAR') {
                    $registerPriceForOneYear = $price['Price']['@attributes']['Price'];
                    break 2; // Exit both loops once the price for 1 year is found
                }
            }
        }
        return $registerPriceForOneYear;
    }   
}

我正在使用的数组的一部分:

Array
(
    [@attributes] => Array
        (
            [Status] => OK
        )

    [Errors] => Array
        (
        )

    [Warnings] => Array
        (
        )

    [RequestedCommand] => namecheap.users.getpricing
    [CommandResponse] => Array
        (
            [@attributes] => Array
                (
                    [Type] => namecheap.users.getPricing
                )

            [UserGetPricingResult] => Array
                (
                    [ProductType] => Array
                        (
                            [@attributes] => Array
                                (
                                    [Name] => domains
                                )

                            [ProductCategory] => Array
                                (
                                    [0] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                    [Name] => reactivate
                                                )

                                            [Product] => Array
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [Name] => com
                                                        )

                                                    [Price] => Array
                                                        (
                                                            [@attributes] => Array
                                                                (
                                                                    [Duration] => 1
                                                                    [DurationType] => YEAR
                                                                    [Price] => 13.48
                                                                    [PricingType] => MULTIPLE
                                                                    [AdditionalCost] => 0.18
                                                                    [RegularPrice] => 15.88
                                                                    [RegularPriceType] => MULTIPLE
                                                                    [RegularAdditionalCost] => 0.18
                                                                    [RegularAdditionalCostType] => MULTIPLE
                                                                    [YourPrice] => 13.48
                                                                    [YourPriceType] => MULTIPLE
                                                                    [YourAdditonalCost] => 0.18
                                                                    [YourAdditonalCostType] => MULTIPLE
                                                                    [PromotionPrice] => 0.0
                                                                    [Currency] => USD
                                                                )

                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [@attributes] => Array
                                                (
                                                    [Name] => register
                                                )

                                            [Product] => Array
                                                (
                                                    [@attributes] => Array
                                                        (
                                                            [Name] => com
                                                        )

                                                    [Price] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [@attributes] => Array
                                                                        (
                                                                            [Duration] => 1
                                                                            [DurationType] => YEAR
                                                                            [Price] => 10.28
                                                                            [PricingType] => ABSOLUTE
                                                                            [AdditionalCost] => 0.18
                                                                            [RegularPrice] => 13.98
                                                                            [RegularPriceType] => MULTIPLE
                                                                            [RegularAdditionalCost] => 0.18
                                                                            [RegularAdditionalCostType] => MULTIPLE
                                                                            [YourPrice] => 10.28
                                                                            [YourPriceType] => ABSOLUTE
                                                                            [YourAdditonalCost] => 0.18
                                                                            [YourAdditonalCostType] => MULTIPLE
                                                                            [PromotionPrice] => 0.0
                                                                            [Currency] => USD
                                                                        )

                                                                )

每当我运行此命令时,我都会收到未定义的数组键错误。 我知道数据肯定存在,因为我可以像这样“硬编码”它:

$domainRegistration = $newArr['CommandResponse']['UserGetPricingResult']['ProductType']['ProductCategory']['2']['Product']['Price']['0']['@attributes']['Price'];

完整的错误在这里:

PHP Warning:  Undefined array key "@attributes" in D:\xampp\htdocs\sm\registrationPrice.php on line 6

Warning: Undefined array key "@attributes" in D:\xampp\htdocs\sm\registrationPrice.php on line 6

Warning: Trying to access array offset on value of type null in D:\xampp\htdocs\sm\registrationPrice.php on line 6

Warning: Undefined array key "@attributes" in D:\xampp\htdocs\sm\registrationPrice.php on line 6

Warning: Trying to access array offset on value of type null in D:\xampp\htdocs\sm\registrationPrice.php on line 6

第 6 行是:

return $category['@attributes']['Name'] === 'register' && $category['Product']['@attributes']['Name'] === $domainTLD;

php array-filter
1个回答
0
投票

过滤器需要结束

$newArr['CommandResponse']['UserGetPricingResult']['ProductType']['ProductCategory']

内部

foreach
循环需要结束
$product['Price']
,而不是
$product

function getRegisterPrice($domainTLD, $newArr) {
    // Extract the "Register" product category for the target TLD
    $registerCategory = array_filter($newArr['CommandResponse']['UserGetPricingResult']['ProductType']['ProductCategory'], function ($category) use ($domainTLD) {
        //var_dump($category);
        $catName = $category['@attributes']['Name'];
        $prodName = $category['Product']['@attributes']['Name'];
        //echo "Cat: $catName\nProduct: $prodName\n";
        return $catName === 'register' && $prodName === $domainTLD;
    });
    //var_dump($registerCategory);
    // Extract the prices for the 1-year duration from the "register" category for the target TLD
    $registerPrices = array_column($registerCategory, 'Product');
    var_dump($registerPrices);
    $registerPriceForOneYear = null;
    foreach ($registerPrices as $product) {
        foreach ($product['Price'] as $price) {
            if ($price['@attributes']['Duration'] == 1 && $price['@attributes']['DurationType'] == 'YEAR') {
                $registerPriceForOneYear = $price['@attributes']['Price'];
                break 2; // Exit both loops once the price for 1 year is found
            }
        }
    }
    return $registerPriceForOneYear;
}

演示

© www.soinside.com 2019 - 2024. All rights reserved.