I'm trying to find the most efficient way to return the productID if I supply an id. For example: If I supply the id 17879, how can I get php to return the productID 12550
What I have is below, but I figure there has to be a way to do it.
Thanks in advance for any suggestions!
$sub_type = 17879; #Could be any id
$pid = 0;
$json = file_get_contents($url);
$obj = json_decode($json, true);
foreach ($obj as $val) {
if (is_array($val)) {
foreach ($val as $val) {
if (is_array($val)) {
foreach ($val as $val) {
if ($val['id'] == $sub_type)
$pid = $val['productID'];
}
}
}
}
}
echo $pid;
$url supplies the following JSON
{
"meta": {
"time": "Thursday, September 24, 2015 7:43:53 PM",
"statusCode": 200
},
"results": {
"errors": [],
"messages": [],
"data": [
{
"id": 17879,
"productID": 12550,
"name": " Bill of Rights 8.5x11 ",
"description": "",
"hasTemplate": true,
"deliveredPrices": [
{
"description": "FedEx Ground",
"price": 84.4,
"country": null,
"countryCode": null,
"created": "2015-02-25T16:14:49.283"
},
{
"description": "FedEx 3 Day",
"price": 164.4,
"country": null,
"countryCode": null,
"created": "2015-02-25T16:14:49.283"
},
{
"description": "FedEx 2 Day",
"price": 224.4,
"country": null,
"countryCode": null,
"created": "2015-02-25T16:14:49.287"
},
{
"description": "FedEx Overnight PM",
"price": 304.4,
"country": null,
"countryCode": null,
"created": "2015-02-25T16:14:49.287"
}
]
},
{
"id": 17880,
"productID": 12558,
"name": "Annual Client Survey 8.5x11 (5 pages)",
"description": "",
"hasTemplate": true,
"deliveredPrices": [
{
"description": "FedEx Ground",
"price": 84.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T13:34:01.933"
},
{
"description": "FedEx 3 Day",
"price": 164.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T13:34:01.933"
},
{
"description": "FedEx 2 Day",
"price": 224.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T13:34:01.937"
},
{
"description": "FedEx Overnight PM",
"price": 304.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T13:34:01.937"
}
]
},
{
"id": 17881,
"productID": 12559,
"name": "Estate Planning 8.5x11 ",
"description": "",
"hasTemplate": true,
"deliveredPrices": [
{
"description": "FedEx Ground",
"price": 84.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T14:19:09.29"
},
{
"description": "FedEx 3 Day",
"price": 164.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T14:19:09.297"
},
{
"description": "FedEx 2 Day",
"price": 224.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T14:19:09.307"
},
{
"description": "FedEx Overnight PM",
"price": 304.4,
"country": null,
"countryCode": null,
"created": "2015-02-26T14:19:09.317"
}
]
},
.......
You get the idea. It keeps going. =)
Well.. narrowing it down a bit you could sort of jump right into the data array and go from there.
$obj = json_decode($json, true);
$sub_type = 17879;
$pid = 0;
foreach($obj['results']['data'] as $data){
if($data['id'] == $sub_type){
$pid = $data['productID'];
}
}
echo $pid; //12550
Is this what you are looking for? Or did I misunderstand the question?
Assuming that you have decoded the JSON into an associative array called $array, build an array with ID mapped to productID:
$array = json_decode($url, true);
$results = array_column($array['results']['data'], 'productID', 'ID');
echo $results[17879]; //12550
PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column()
Related
I'm very new to the world of APIs and am currently trying to display all reviews I have on my reviews.io account for a specific product. I've tried to make the following script, which now seems to work, however, I would love to have the results sorted nicely in a for-each loop.
Here my current script:
<?php
$header_data = array(
'store' => 'MYSHOP',
'apikey' => 'MYKEY',
'method' => 'GET'
);
$ch = curl_init();
$curlOpts = array(
CURLOPT_URL => 'https://api.reviews.co.uk/product/review?store=MYSHOP&sku=TSCB20&apikey=MYKEY',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => false
);
curl_setopt_array($ch, $curlOpts);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
$rv = curl_exec($ch);
$data = json_decode($rv,true);
curl_close($ch);
?>
This outputs all reviews in Raw code or JSON I believe, however, I would like to have them displayed nicely in a div for each item.
The output is as follows (formatted):
{
"store": {
"name": "",
"logo": ""
},
"stats": {
"average": "5.0000",
"count": 2
},
"reviews": {
"total": 2,
"per_page": 15,
"current_page": 1,
"last_page": 1,
"from": 1,
"to": 2,
"data": [
{
"votes": null,
"flags": null,
"title": "David",
"product_review_id": 2978412,
"review": "...is beautiful and I had a great time on this tour. Already started to plan my next trip with them",
"sku": "TSCB20",
"rating": 5,
"date_created": "",
"order_id": "",
"timeago": "",
"reviewer": {
"first_name": "David",
"last_name": "",
"verified_buyer": "yes",
"address": "",
"profile_picture": "",
"gravatar": "38677a4e8a55189055d6e5bf2efa9ade"
},
"ratings": [],
"replies": [],
"images": [],
"product": {
"sku": "TSCB20",
"name": "",
"description": "",
"link": "",
"image_url": "",
"mpn": "",
"brand": null,
"category": null,
"custom": null
},
"author": {
"email": "david#"
}
},
{
"votes": null,
"flags": null,
"title": null,
"product_review_id": 2978009,
"review": "Exceeded expectations on all fronts cultural and Food ",
"sku": "TSCB20",
"rating": 5,
"date_created": "",
"order_id": "RGFY4ZG",
"timeago": "",
"reviewer": {
"first_name": "Alan",
"last_name": "",
"verified_buyer": "yes",
"address": "",
"profile_picture": "",
"gravatar": "64e2ac644a158b76a82f9e1c5c2886f5"
},
"ratings": [],
"replies": [],
"images": [],
"product": {
"sku": "TSCB20",
"name": "",
"description": "",
"link": "",
"image_url": "",
"mpn": "",
"brand": null,
"category": null,
"custom": null
},
"author": {
"email": ""
}
}
]
},
"ratings": [],
"settings": {
"write_review_button": 1,
"disable_product_seo_css": 0,
"show_product_review_titles": 0
},
"word": "Excellent",
"products": [
{
"sku": "TSCB20",
"name": ""
}
],
"write_review_link": ""
}
My ideal outcome now would be a loop, that fetches the reviewer's name, the review itself and the star-rating. e.g.
<p class="review">Review here</p>
<p class="reviewer">Name here</p>
<p class="rating">Rating here</p>
So technically based on the current output, I should have 2 reviews. Some expert help would be greatly appreciated as I am very new to APIs. Thank you very much in advance.
Simple foreach() will do the job:
<?php foreach($data['reviews']['data'] as $dat){?>
<p class="review"><?php echo $dat['review'];?></p>
<p class="reviewer"><?php echo trim($dat['reviewer']['first_name'].' '.$dat['reviewer']['last_name']);?></p>
<p class="rating"><?php echo $dat['rating'];?></p>
<?php }?>
I need your help..I would like to call a json value using a json value...
here's my sample json...
first json:
{
"first_name": "Richard Catibog",
"last_name": "",
"display_name": "Richard Catibog ",
"roles": {
"owner": false,
"administrator": true
},
"login_count": 108,
"enabled": true,
"departments": [
1817719575,
1817719887,
1817722991
],
"id": 4142095,
"last_login": "2017-07-12T10:03:57Z",
"create_date": "2017-04-28T12:09:57Z",
"email": "richard.catibog+21757948948#51talk.com"
},
{
"first_name": "Rovi Cruz",
"last_name": "",
"display_name": "Rovi Roy Cruz ",
"roles": {
"owner": false,
"administrator": true
},
"login_count": 98,
"enabled": true,
"departments": [
1817719575,
1817719887,
1817722991
],
"id": 4225009,
"last_login": "2017-07-13T00:37:27Z",
"create_date": "2017-06-05T06:15:49Z",
"email": "rovi.cruz+21821290968#51talk.com"
}
Second json:
[
{
"description": "Fees Support for American Academy",
"settings": {
},
"enabled": false,
"id": 1817719575,
"name": "AA Fees Support [L2]"
},
{
"description": "American Academy",
"settings": {
},
"enabled": true,
"id": 1817722991,
"name": "Lesson and Fee Support"
},
{
"description": "Technical Support",
"settings": {
},
"enabled": true,
"id": 1817719887,
"name": "Technical Support"
}
]
I would like to call the id on the second and echo the names using the departments id list on first json...
here's what i get...
but the result that i want to see is AA Fees Support [L2], Lesson and Fee Support and Technical Support...
thank you, hope you can help me...
In this I made an array $desc that is keyed on the departments id numbers so you can easily convert the id to a Descrition
$js1 = '{
"first_name": "Richard Catibog", "last_name": "",
"display_name": "Richard Catibog ",
"roles": { "owner": false,"administrator": true},
"login_count": 108,"enabled": true,
"departments": [1817719575,1817719887,1817722991],
"id": 4142095,"last_login": "2017-07-12T10:03:57Z",
"create_date": "2017-04-28T12:09:57Z",
"email": "richard.catibog+21757948948#51talk.com"
}';
$js2 = '[
{
"description": "Fees Support for American Academy",
"settings": {},"enabled": false,"id": 1817719575,
"name": "AA Fees Support [L2]"
},
{
"description": "American Academy",
"settings": {},"enabled": true,"id": 1817722991,
"name": "Lesson and Fee Support"
},
{
"description": "Technical Support",
"settings": {},"enabled": true,"id": 1817719887,
"name": "Technical Support"
}
]';
$first = json_decode($js1);
//print_r($first);
$second = json_decode($js2);
//print_r($second);
// make array key'd on the id
$desc = [];
foreach ($second as $s) {
$desc[$s->id] = $s;
}
print_r($desc);
// go through all the dept id's in the first array
// getting a matching desctipyion from array 2
foreach ( $first->departments as $dept) {
echo $desc[$dept]->description . PHP_EOL;
}
UPDATE: After the first json data structure got changed to an array of objects
NOTE I changed your first json data structure to make it valid JSON by adding [] around the 2 objects to make a valid JSON data structure i.e. an array of objects.
All you need to do in this case is add a loop to lop over the now array of objects.
$js1 = '[{
"first_name": "Richard Catibog",
"last_name": "",
"display_name": "Richard Catibog ",
"roles": { "owner": false, "administrator": true },
"login_count": 108,
"enabled": true,
"departments": [ 1817719575, 1817719887, 1817722991 ],
"id": 4142095,
"last_login": "2017-07-12T10:03:57Z",
"create_date": "2017-04-28T12:09:57Z",
"email": "richard.catibog+21757948948#51talk.com"
},
{
"first_name": "Rovi Cruz",
"last_name": "",
"display_name": "Rovi Roy Cruz ",
"roles": { "owner": false, "administrator": true },
"login_count": 98,
"enabled": true,
"departments": [ 1817719575, 1817719887, 1817722991 ],
"id": 4225009,
"last_login": "2017-07-13T00:37:27Z",
"create_date": "2017-06-05T06:15:49Z",
"email": "rovi.cruz+21821290968#51talk.com"
}]';
$js2 = '[
{
"description": "Fees Support for American Academy",
"settings": {},"enabled": false,"id": 1817719575,
"name": "AA Fees Support [L2]"
},
{
"description": "American Academy",
"settings": {},"enabled": true,"id": 1817722991,
"name": "Lesson and Fee Support"
},
{
"description": "Technical Support",
"settings": {},"enabled": true,"id": 1817719887,
"name": "Technical Support"
}
]';
$first = json_decode($js1);
//print_r($first);
$second = json_decode($js2);
//print_r($second);
// make array key'd on the id
$desc = [];
foreach ($second as $s) {
$desc[$s->id] = $s;
}
//print_r($desc);
//Loop through all the first array of object
foreach ( $first as $f) {
// go through all the dept id's in the first array
// getting a matching desctipyion from array 2
foreach ( $f->departments as $dept) {
echo $desc[$dept]->description . PHP_EOL;
}
echo PHP_EOL;
}
RESULT
Fees Support for American Academy
Technical Support
American Academy
Fees Support for American Academy
Technical Support
American Academy
here you go
$json1 = '{
"first_name": "Richard Catibog",
"last_name": "",
"display_name": "Richard Catibog ",
"roles": {
"owner": false,
"administrator": true
},
"login_count": 108,
"enabled": true,
"departments": [
1817719575,
1817719887,
1817722991
],
"id": 4142095,
"last_login": "2017-07-12T10:03:57Z",
"create_date": "2017-04-28T12:09:57Z",
"email": "richard.catibog+21757948948#51talk.com"
}
';
$json2 ='[
{
"description": "Fees Support for American Academy",
"settings": {
},
"enabled": false,
"id": 1817719575,
"name": "AA Fees Support [L2]"
},
{
"description": "American Academy",
"settings": {
},
"enabled": true,
"id": 1817722991,
"name": "Lesson and Fee Support"
},
{
"description": "Technical Support",
"settings": {
},
"enabled": true,
"id": 1817719887,
"name": "Technical Support"
}
]';
$json1 = json_decode($json1, true);
$json2 = json_decode($json2, true);
$department = function($id) use($json2) {
foreach($json2 as $dep) {
if($dep["id"] == $id) {
return $dep;
}
}
return false;
};
foreach($json1["departments"] as $id) {
$dep = $department($id);
if(is_array($dep)) {
echo sprintf("id: %s, name: %s\n", $id, $dep["name"]);
}
}
i want to fetch value from object. it should start from shirts. it should also check whether it is mega_menu or not. if it is mega menu, show its children. also depend on megamenu value, it should apply class 'parent'.
{
"1": {
"title": "Root Catalog",
"url": null,
"id": "1",
"img_url": null,
"mega_menu": "true",
"children": {
"2": {
"title": "Default Category",
"url": null,
"id": "2",
"img_url": null,
"mega_menu": "true",
"children": {
"4": {
"title": "shirts",
"url": "shirts",
"id": "4",
"img_url": null,
"mega_menu": "false"
},
"8": {
"title": "qme31q",
"url": "dresses",
"id": "8",
"img_url": null,
"mega_menu": "true",
"children": {
"20": {
"title": "srtysryt",
"url": "srtysryt",
"id": "20",
"img_url": null,
"mega_menu": "false"
}
}
},
"11": {
"title": "Jackets + Sweaters",
"url": "jackets-sweaters",
"id": "11",
"img_url": null,
"mega_menu": "false"
},
"12": {
"title": "Accessories",
"url": "accessories",
"id": "12",
"img_url": null,
"mega_menu": "true",
"children": {
"26": {
"title": "a",
"url": "a",
"id": "26",
"img_url": null,
"mega_menu": "false"
}
}
},
"13": {
"title": "Plus",
"url": "plus",
"id": "13",
"img_url": null,
"mega_menu": "false"
},
"22": {
"title": "abcde",
"url": "abcde",
"id": "22",
"img_url": null,
"mega_menu": "true",
"children": {
"23": {
"title": "dduy",
"url": "dduy",
"id": "23",
"img_url": null,
"mega_menu": "false"
}
}
},
"24": {
"title": "1",
"url": "1",
"id": "24",
"img_url": null,
"mega_menu": "true",
"children": {
"25": {
"title": "1.1",
"url": "1-1",
"id": "25",
"img_url": null,
"mega_menu": "false"
}
}
}
}
},
"16": {
"title": "test",
"url": "test",
"id": "16",
"img_url": null,
"mega_menu": "false"
},
"18": {
"title": "rest",
"url": "rest",
"id": "18",
"img_url": null,
"mega_menu": "false"
},
"29": {
"title": "q",
"url": "q",
"id": "29",
"img_url": null,
"mega_menu": "false"
}
}
}
}
Below code convert your array to Ul li list.
// Build parent ul
function BuildMegaMenu($arr,$class = 'parent')
{
if(!empty($arr))
{
$html = "<ul class='p_category'>";
foreach($arr as $val)
{
if($val['mega_menu'] && !empty($val['children'])) // check for mega menu and children
{
// call sub helper function for procesing the children of children
// making a ul li with class
$rs = SubBuildMegaMenu($val['children'], 'parent');
if(!empty($rs)) {
$html .= "<li>";
$html .= "<div class='".$class."'>";
$html .= $val['title'];
$html .= $rs;
$html .= "</div>";
$html .= "</li>";
}
}
else {
$html .= "<li><div class='parent'>".$val['title']."</div></li>";
}
}
$html .= "</ul>";
return $html;
}
return false;
}
// Sub helper function for processing childrens
function SubBuildMegaMenu($arr, $class = 'parent')
{
if(!empty($arr))
{
$html = "<ul>";
foreach($arr as $val)
{
if($val['mega_menu'] && !empty($val['children'])) // check for mega menu and children
{
// call sub helper function for procesing the children of children
// making a ul li with class
$rs = SubBuildMegaMenu($val['children'], $class);
if(!empty($rs))
{
$html .= "<li>";
$html .= "<div class='".$class."'>";
$html .= $val['title'];
$html .= $rs;
$html .= "</div>";
$html .= "</li>";
}
}
else{
$html .= "<li><div class='parent'>".$val['title']."</div></li>";
}
}
$html .= "</ul>";
return $html;
}
}
$data = '{
"1": {
"title": "Root Catalog",
"url": null,
"id": "1",
"img_url": null,
"mega_menu": "true",
"children": {
"2": {
"title": "Default Category",
"url": null,
"id": "2",
"img_url": null,
"mega_menu": "true",
"children": {
"4": {
"title": "shirts",
"url": "shirts",
"id": "4",
"img_url": null,
"mega_menu": "false"
},
"8": {
"title": "qme31q",
"url": "dresses",
"id": "8",
"img_url": null,
"mega_menu": "true",
"children": {
"20": {
"title": "srtysryt",
"url": "srtysryt",
"id": "20",
"img_url": null,
"mega_menu": "false"
}
}
},
"11": {
"title": "Jackets + Sweaters",
"url": "jackets-sweaters",
"id": "11",
"img_url": null,
"mega_menu": "false"
},
"12": {
"title": "Accessories",
"url": "accessories",
"id": "12",
"img_url": null,
"mega_menu": "true",
"children": {
"26": {
"title": "a",
"url": "a",
"id": "26",
"img_url": null,
"mega_menu": "false"
}
}
},
"13": {
"title": "Plus",
"url": "plus",
"id": "13",
"img_url": null,
"mega_menu": "false"
},
"22": {
"title": "abcde",
"url": "abcde",
"id": "22",
"img_url": null,
"mega_menu": "true",
"children": {
"23": {
"title": "dduy",
"url": "dduy",
"id": "23",
"img_url": null,
"mega_menu": "false"
}
}
},
"24": {
"title": "1",
"url": "1",
"id": "24",
"img_url": null,
"mega_menu": "true",
"children": {
"25": {
"title": "1.1",
"url": "1-1",
"id": "25",
"img_url": null,
"mega_menu": "false"
}
}
}
}
},
"16": {
"title": "test",
"url": "test",
"id": "16",
"img_url": null,
"mega_menu": "false"
},
"18": {
"title": "rest",
"url": "rest",
"id": "18",
"img_url": null,
"mega_menu": "false"
},
"29": {
"title": "q",
"url": "q",
"id": "29",
"img_url": null,
"mega_menu": "false"
}
}
}}';
$convertedArr = json_decode($data, true);
echo '<pre>';print_r(BuildMegaMenu($convertedArr));die;
I have really have troubles trying to work out why this is not working, and it seems like it should be easy, but just cannot seem to get it.
All I am looking to do is grab the sku field (which is VBP-01 below).
{
"variants": [
{
"id": 2314578,
"created_at": "2014-07-29T07:22:18.921Z",
"updated_at": "2015-05-21T15:42:42.136Z",
"product_id": 1188647,
"default_ledger_account_id": null,
"buy_price": "124.0",
"committed_stock": "0",
"incoming_stock": "3",
"composite": false,
"description": null,
"is_online": false,
"keep_selling": false,
"last_cost_price": "124.0",
"manage_stock": true,
"max_online": null,
"moving_average_cost": "124",
"name": "Lanparte battery pinch VBP-01",
"online_ordering": false,
"opt1": null,
"opt2": null,
"opt3": null,
"position": 1,
"product_name": "Lanparte V-Mount Battery Pinch",
"product_status": "active",
"product_type": null,
"retail_price": "0.0",
"sellable": true,
"sku": "VBP-01",
"status": "active",
"stock_on_hand": "1",
"supplier_code": "VBP-01",
"taxable": true,
"upc": null,
"weight": null,
"wholesale_price": "0.0",
"image_ids": [],
"variant_prices": [
{
"price_list_id": "buy",
"value": "124.0"
},
{
"price_list_id": "retail",
"value": "0.0"
},
{
"price_list_id": "wholesale",
"value": "0.0"
}
],
"locations": [
{
"location_id": 16377,
"stock_on_hand": "1",
"committed": null,
"incoming": "3",
"bin_location": null,
"reorder_point": 3
}
],
"prices": {
"buy": "124.0",
"retail": "0.0",
"wholesale": "0.0"
},
"stock_levels": {
"16377": "1.0"
},
"committed_stock_levels": {},
"incoming_stock_levels": {
"16377": "3.0"
}
}
],
"meta": {
"total": 1
}
}
Currently I using the following code with no luck
$url = "https://api.tradegecko.com/variants?sku=VBP-01";
$data = file_get_contents($url, false, $context);
$json = json_decode($data);
print $json->{'variant'}->{'sku'};
What I am doing wrong?
Just
echo $json->{'variants'}[0]->{'sku'};
In a loop
foreach ($json->variants as $variants) {
echo $variants->sku;
}
I am workin in PHP/MYSql application. i am geting data in php like follow
[
{
"id": "4",
"rawId": "4",
"displayName": "123 456",
"name": {
"familyName": "456",
"formatted": "123 456",
"givenName": "123"
},
"nickname": null,
"phoneNumbers": null,
"emails": null,
"addresses": null,
"ims": [
{
"type": -1,
"value": ".adgjm",
"id": "8",
"pref": false
}
],
"organizations": null,
"birthday": null,
"note": null,
"photos": null,
"categories": null,
"urls": null
},
{
"id": "5",
"rawId": "5",
"displayName": "Dooney Evans",
"name": {
"middleName": "",
"familyName": "Evans",
"formatted": "Dooney Evans",
"givenName": "Dooney"
},
"nickname": null,
"phoneNumbers": [
{
"type": "work",
"value": "512-555-1234",
"id": "11",
"pref": false
}
],
"emails": null,
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": null,
"photos": null,
"categories": null,
"urls": null
},
{
"id": "18",
"rawId": "18",
"displayName": "John Doe",
"name": {
"familyName": "Doe",
"formatted": "John Doe",
"givenName": "John"
},
"nickname": null,
"phoneNumbers": null,
"emails": null,
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": null,
"photos": null,
"categories": null,
"urls": null
},
{
"id": "19",
"rawId": "19",
"displayName": "Rob Doe",
"name": {
"familyName": "Doe",
"formatted": "Rob Doe",
"givenName": "Rob"
},
"nickname": null,
"phoneNumbers": null,
"emails": null,
"addresses": null,
"ims": null,
"organizations": null,
"birthday": null,
"note": null,
"photos": null,
"categories": null,
"urls": null
}
]
Currently it has key in key have may value. may another array, may object and sometimes also array and object goes more nested.
For now i am doing this php to display properly
is there another good or proper way?
$data = json_decode($data);
if(is_array($data))
{
echo '<pre>';
for($i = 0; $i< count($data); $i++)
{
$record = $data[$i];
foreach($record as $key => $value)
{
if($value)
if(is_object($value))
{
foreach($value as $key1 => $value1)
{
echo $key1." = ".$value1."<br />";
}
}
else if (is_array($value))
{
for($j = 0; $j< count($value); $j++)
{
$innerValue = $value[$j];
if(is_object($innerValue))
{
foreach($innerValue as $key1 => $value1)
{
echo $key1." = ".$value1."<br />";
}
}
else if (is_array($innerValue))
{
}
else
{
echo $key." = ".$value."<br />";
}
}
}
else
{
echo $key." = ".$value."<br />";
}
}
//print_r($record);
}
}
You could try using a recursive function, instead of nesting that much:
Pass the function an Array, the output of json_decode($json) for example.
function print_json($json) {
if (is_array($json) || is_object($json)) {
echo "<table width=100%>";
$type = 'Array';
if(is_object($json)) $type = 'Object';
echo '<tr><td colspan=2 style="background-color:#333333;">
<strong><font color=white>'.$type.'</font></strong>
</td></tr>';
foreach ($json as $k => $v) {
echo '<tr><td valign="top" style="background-color:#F0F0F0;">';
echo '<strong>'.$k.'</strong></td><td>';
print_json($v);
echo "</td></tr>";
}
echo "</table>";
return;
}
echo $json;
}
Run in PHP Fiddle