i have a pretty weird json here. Im trying to iterate through every subscription
object and get some data
Here is the json
{
"result":"success",
"data":{
"subscriptions":[
{
"name":"app_generator",
"expirey":1628188363942,
"hwids":3,
"hwids_used":[
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515",
"efede5757f512f9eae1d600302ca5c4e6a3cadb0658310c91c6da2ada68175f4"
]
},
{
"name":"apple",
"expirey":1625596518277,
"hwids":2,
"hwids_used":[
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515",
"dc253a62ea65cd92c6cb26f15330cf4a8f9e5c3ecf7caae161a3f97de4c84515a"
]
}
]
}
}
The code i tried was:
foreach($json['result']['data']['subscriptions'] as $a)
{
//print_r($a);
$expirey = $a['expirey']
}
Im just getting a lot of errors. Let me know what the correct approach would be. My goal is to mainly get the name
& expirey
data
My Answer:
"subscriptions": [
{
"id": 1,
"name": "Subscription 1",
"price": 10,
"status": "active"
},
{
"id": 2,
"name": "Subscription 2",
"price": 20,
"status": "inactive"
},
{
"id": 3,
"name": "Subscription 3",
"price": 30,
"status": "active"
}
]
}
To iterate through each subscription object in the "subscriptions" array, you can use a foreach loop in PHP like this:
php
$json = '{
"subscriptions": [
{
"id": 1,
"name": "Subscription 1",
"price": 10,
"status": "active"
},
{
"id": 2,
"name": "Subscription 2",
"price": 20,
"status": "inactive"
},
{
"id": 3,
"name": "Subscription 3",
"price": 30,
"status": "active"
}
]
}';
$data = json_decode($json);
foreach ($data->subscriptions as $subscription) {
echo "Subscription ID: " . $subscription->id . "
";
echo "Subscription Name: " . $subscription->name . "
";
echo "Subscription Price: $" . $subscription->price . "
";
echo "Subscription Status: " . $subscription->status . "
";
}
This code will iterate through each subscription object in the "subscriptions" array and output the ID, name, price, and status of each subscription.
Rate this post
3 of 5 based on 4167 votesComments