Search code examples
phparray-filter

php array_filter issues, trying to get data out, but its not working


I'm pretty new to PHP and I'm trying to figure out the array_filter, and why its not seeming to work for me. I've got a large array from the namecheap sandbox to test and learn with from a colleague of mine.

So i'm passing the array and a specified domain TLD into my function, and i'm expecting a single Price to be returned from the 'register' product category.

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;
    }   
}

Part of the array I'm using:

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
                                                                        )

                                                                )

I keep Getting the undefined array key error whenever I run this. I know the data definitely exists because I can "hard code" it like this:

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

The full error is here:

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

line 6 is: return $category['@attributes']['Name'] === 'register' && $category['Product']['@attributes']['Name'] === $domainTLD;


Solution

  • The filter needs to be over $newArr['CommandResponse']['UserGetPricingResult']['ProductType']['ProductCategory'].

    The inner foreach loop needs to be over $product['Price'], not $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;
    }
    

    DEMO