fork download
  1. <?php
  2. $arr = json_decode('{"@attributes":{"Status":"OK"},"Errors":{},"Warnings":{},"RequestedCommand":"namecheap.users.getpricing","CommandResponse":{"@attributes":{"Type":"namecheap.users.getPricing"},"UserGetPricingResult":{"ProductType":{"@attributes":{"Name":"domains"},"ProductCategory":[{"@attributes":{"Name":"reactivate"},"Product":{"@attributes":{"Name":"com"},"Price":{"@attributes":{"Duration":"1","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}}}},{"@attributes":{"Name":"register"},"Product":{"@attributes":{"Name":"com"},"Price":[{"@attributes":{"Duration":"1","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"2","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"3","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"4","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"5","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"6","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"7","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"8","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"9","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"10","DurationType":"YEAR","Price":"13.98","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.98","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.98","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}}]}},{"@attributes":{"Name":"renew"},"Product":{"@attributes":{"Name":"com"},"Price":[{"@attributes":{"Duration":"1","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"2","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"3","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"4","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"5","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"6","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"7","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"8","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}},{"@attributes":{"Duration":"9","DurationType":"YEAR","Price":"14.58","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"14.58","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"14.58","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}}]}},{"@attributes":{"Name":"transfer"},"Product":{"@attributes":{"Name":"com"},"Price":{"@attributes":{"Duration":"1","DurationType":"YEAR","Price":"13.78","PricingType":"MULTIPLE","AdditionalCost":"0.18","RegularPrice":"13.78","RegularPriceType":"MULTIPLE","RegularAdditionalCost":"0.18","RegularAdditionalCostType":"MULTIPLE","YourPrice":"13.78","YourPriceType":"MULTIPLE","YourAdditonalCost":"0.18","YourAdditonalCostType":"MULTIPLE","PromotionPrice":"0.0","Currency":"USD"}}}}]}}},"Server":"PHX01SBAPIEXT06","GMTTimeDifference":"--4:00","ExecutionTime":"0.065"}', true);
  3.  
  4. function getRegisterPrice($domainTLD, $newArr) {
  5. // Extract the "Register" product category for the target TLD
  6. $registerCategory = array_filter($newArr['CommandResponse']['UserGetPricingResult']['ProductType']['ProductCategory'], function ($category) use ($domainTLD) {
  7. //var_dump($category);
  8. $catName = $category['@attributes']['Name'];
  9. $prodName = $category['Product']['@attributes']['Name'];
  10. //echo "Cat: $catName\nProduct: $prodName\n";
  11. return $catName === 'register' && $prodName === $domainTLD;
  12. });
  13. //var_dump($registerCategory);
  14. // Extract the prices for the 1-year duration from the "register" category for the target TLD
  15. $registerPrices = array_column($registerCategory, 'Product');
  16. //var_dump($registerPrices);
  17. $registerPriceForOneYear = null;
  18. foreach ($registerPrices as $product) {
  19. foreach ($product['Price'] as $price) {
  20. if ($price['@attributes']['Duration'] == 1 && $price['@attributes']['DurationType'] == 'YEAR') {
  21. $registerPriceForOneYear = $price['@attributes']['Price'];
  22. break 2; // Exit both loops once the price for 1 year is found
  23. }
  24. }
  25. }
  26. return $registerPriceForOneYear;
  27. }
  28.  
  29. var_dump(getRegisterPrice('com', $arr));
  30.  
  31.  
Success #stdin #stdout 0.02s 26092KB
stdin
Standard input is empty
stdout
string(5) "13.98"