#Trying to learn how to get data from nested json arrays.
1 messages · Page 1 of 1 (latest)
Hi @gaunt token, I'm trying to get it to give me a single "price" back.
what does "flatten the array" mean?
the post shows me that original message was deleted, can you see my post?
I can't
shoot
Ok, let me see if I can do it in parts here.
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.
<?php
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;
}
}
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'];
I've tried many permutations and different key sets, but i just wont work.
Any help you could provide would be greatly appreciated.
What does the $newArr look like?
@kindred quest
For safety reasons we do not allow files with certain file extensions.
I cant post the whole this because its massive, But the beginning for the .com TLD looks like the attached message.
You can share your code using triple backticks like this:
```
YOUR CODE
```
For longer scripts use Hastebin or GitHub Gists and share the link here
- message.txt
Linking to the namecheap docs is fine as well I guess, weird bot
its an api response coded from xml to json, lets see if I can get a snippet working
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
)```
the [@attributes] and [Price] are both under [Product]
You're basically trying to filter the array $newArr['CommandResponse']['UserGetPricingResult']
$pricingResults = $newArr['CommandResponse']['UserGetPricingResult'];
$filteredResults = array_filter($pricingResults, function ($result) use $(domainTLD) {
// print out the result here, then check how you can filter the
}):