0

I am sure this is a complete noob question but I am trying to iterate through an api response from stripe.com and I am able to echo out the array in php to be something like this:

{ "count": 3, "data": [ { "amount": 29900, "object": "plan", "interval": "month", "livemode": false, "currency": "usd", "name": "vb Group unlimited", "id": "vb-std-group2" }, { "amount": 9900, "object": "plan", "interval": "year", "livemode": false, "currency": "usd", "name": "vb Group to 20", "id": "vb-std-group" }, { "amount": 1900, "object": "plan", "interval": "year", "livemode": false, "currency": "usd", "name": "vb-Individual", "id": "vb-std-individual" } ] } 

What I'm trying to do is echo out the "data" array info. Thanks for any insight.

3 Answers 3

1

The string you have is JSON encoded. Decode it using json_decode into a PHP object, say $obj. Then, $obj['data'] is an array. Iterate over it using a foreach loop.

foreach($obj['data'] as $key=>$object) foreach($object as $key=>$value) echo $key . " : " . $value; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @xbonez. I was missing the json_decode. That got it.
0

Try something like this:

$reponseData = $response['data']; foreach($reponseData as $eachDataObj) { foreach($eachDataObj as $key => $val) { echo $key . " => ".$val; } } 

This assumes that $response is already an object, if it is actually just TEXT then you have to use

$response = json_decode($response); 

Comments

0

Check this out it would help you

$myjson = '{ "count": 3, "data": [ { "amount": 29900, "object": "plan", "interval": "month", "livemode": false, "currency": "usd", "name": "vb Group unlimited", "id": "vb-std-group2" }, { "amount": 9900, "object": "plan", "interval": "year", "livemode": false, "currency": "usd", "name": "vb Group to 20", "id": "vb-std-group" }, { "amount": 1900, "object": "plan", "interval": "year", "livemode": false, "currency": "usd", "name": "vb-Individual", "id": "vb-std-individual" } ] }'; $data =json_decode($myjson); $arrData = get_object_vars($data); foreach($arrData as $key=>$value){ $arrData[$key] = $value; } //print_r($arrData['data']); foreach($arrData['data'] as $ke=>$va): foreach($va as $k=>$v): echo $k."-".$v."<br/>"; endforeach; endforeach; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.