List multi dimentional array - php

I have done a query for getting all data's under same id from other table.There are more than 2 data's under same id.
I need to display all data 's under same id like an array.
Here is my sql query:
"SELECT incident.*,entry.*,fighter.*
FROM register_incident AS incident JOIN
register_entry_points AS entry
ON entry.incident_id = incident.incident_id
JOIN add_fire_fighters AS fighter
ON entry.entrypoint_id = fighter.entry_point_id
WHERE incident.incident_id=:incident_id"
I get a response like,
"data":[
{
"incident_id": "5",
"user_id": null,
"entrypoint_id": "20",
"entry_points": "New Entry1111",
"comments": "Comment1",
"fighter_id": "67",
"entry_point_id": "20",
"cylpressure": null,
"time_in": null,
"time_out": null,
"duration": null,
"notes": null
},
{
"incident_id": "5",
"user_id": "16",
"entrypoint_id": "20",
"entry_points": "New Entry1111",
"comments": "Comment1",
"fighter_id": "68",
"entry_point_id": "20",
"cylpressure": "300",
"time_in": "10:30:00",
"time_out": "11:45:00",
"duration": "01:15",
"notes": "Test"
},
But i need to display it like,
"data": [
{
"incident_id": "5",
"user_id": null,
"entrypoint_id": "20",
"entry_points": "New Entry1111",
"comments": "Comment1",
"fighter":{
{
"fighter_id": "67",
"entry_point_id": "20",
"cylpressure": null,
"time_in": null,
"time_out": null,
"duration": null,
"notes": null
},
{
"fighter_id": "68",
"entry_point_id": "20",
"cylpressure": null,
"time_in": null,
"time_out": null,
"duration": null,
"notes": null
}
}
}]
How it is possible?

If I get a ResultSet out of a Query with one or more 1:n relationships, I take the result like this sample.
$Result = array();
foreach($Records as $Record) {
// key 1
$key1 = $Record['incident_id'];
if(isset($Result[$key1])) {
$cuIncident=$Result[$key1];
}
else {
$cuIncident=array(
'incident_id' => $key1,
'user_id' => $Record['user_id'],
//......
'fighter' => array()
);
$Result[$key1] = $cuIncident;
}
// key 2
$key2 = $Record['fighter_id'];
if(isset($cuIncident['fighter'][$key2])) {
$cuFighter = $cuIncident['fighter'][$key2];
}
else {
$cuFighter = array(
'fighter_id' => $key2,
'entry_point_id' => $Record['entry_point_id'],
//......
'key3array' => array()
);
$cuIncident['fighter'][$key2] = $cuFighter;
}
// key 3
// ....
If a key is a multi value key, you have to combine these keys like:
$key3 = $Record['key3prop1']."/".$Record['key3prop2'];
if(isset($key3Array[$key3])) ......

Related

Create parent child relation JSON tree

This is my code I have three table posts table, comments table, and likes table. I have a left joined all three tables with postid common in all three tables. I'm getting all data perfectly but I'm getting repeating data of postdata (ex: Postid=1) I don't want that. My problem is getting repeating data of Postdata (ex: Postid=1) I want to make a parent of Postdata where postsdata=1 and I want to insert all data into parent
<?php
include "../dcon.php";
$sql = "select * FROM posts left join comments on comments.Cpostid=posts.PostId left join likes on likes.postid=posts.PostId";
$query=mysqli_query($mysqli,$sql);
$result = mysqli_num_rows($query);
$menus=array();
for($i=0; $i<$result; $i++){
$row = mysqli_fetch_all($query,MYSQLI_ASSOC);
foreach($row as $index => $row ){
$menus[]=[
'PostId'=>$row['PostId'],
'PostVideo'=>$row['PostVideo'],
'PostDesc'=>$row['PostDesc'],
'PostImage'=>$row['PostImage'],
'PostedBy'=>$row['PostedBy'],
'UserName'=>$row['UserName'],
'Status'=>$row['Status'],
'PostParent'=>$row['PostParent'],
'likes'=>$row['likes'],
'comments'=>[
$index => [
'commentsid'=>$row['commentsId'],
'comments'=>$row['comments'],
'Cpostid'=>$row['Cpostid'],
'time'=>$row['time'],
],],
'likes'=>[
$index =>[
'Likeid'=>$row['Lid'],
'dislikes'=>$row['dislikes'],
'postid'=>$row['postid'],
'userid'=>$row['userid'],
]]
];
}
}
echo json_encode($menus,JSON_PRETTY_PRINT);
?>
My output was below:
[
{
"PostId": "1",
"PostVideo": "v2.mp4",
"PostDesc": "this is a tutorial on bench press",
"PostImage": "null",
"PostedBy": "Irfan khan",
"UserName": "irfan",
"Status": "1",
"PostParent": null,
"likes": {
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
"comments": {
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
}
},
{
"PostId": "1",
"PostVideo": "v2.mp4",
"PostDesc": "this is a tutorial on bench press",
"PostImage": "null",
"PostedBy": "Irfan khan",
"UserName": "khan",
"Status": "1",
"PostParent": null,
"likes": {
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
"comments": {
"commentsid": "2",
"comments": "good workouts",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
}
},
{
"PostId": "1",
"PostVideo": "v1.mp4",
"PostDesc": "Chicken generally includes low fat in the meat itself (castrated roosters excluded). The fat is highly concentrated on the skin. A 100g serving of baked chicken breast contains 4 grams of fat and 31 grams of protein, compared to 10 grams of fat and 27 grams of protein for the same portion of broiled, lean skirt steak.",
"PostImage": "im1",
"PostedBy": "Rehmat",
"UserName": null,
"Status": "1",
"PostParent": null,
"likes": {
"Likeid": "20",
"dislikes": "0",
"postid": "1",
"userid": "4"
},
"comments": {
"commentsid": null,
"comments": null,
"Cpostid": null,
"time": null
}
},
{
"PostId": "1",
"PostVideo": "v1.mp4",
"PostDesc": "Chicken generally includes low fat in the meat itself (castrated roosters excluded). The fat is highly concentrated on the skin. A 100g serving of baked chicken breast contains 4 grams of fat and 31 grams of protein, compared to 10 grams of fat and 27 grams of protein for the same portion of broiled, lean skirt steak.",
"PostImage": "im1",
"PostedBy": "Rehmat",
"UserName": null,
"Status": "1",
"PostParent": null,
"likes": {
"Likeid": "26",
"dislikes": "0",
"postid": "1",
"userid": "4"
},
"comments": {
"commentsid": null,
"comments": null,
"Cpostid": null,
"time": null
}
},
{
"PostId": "3",
"PostVideo": null,
"PostDesc": "this is an image of chicken",
"PostImage": "post.jpg",
"PostedBy": "khan",
"UserName": null,
"Status": "0",
"PostParent": null,
"likes": {
"Likeid": null,
"dislikes": null,
"postid": null,
"userid": null
},
"comments": {
"commentsid": null,
"comments": null,
"Cpostid": null,
"time": null
}
},
{
"PostId": "9",
"PostVideo": null,
"PostDesc": "gdhgh",
"PostImage": "ghghdf",
"PostedBy": null,
"UserName": null,
"Status": null,
"PostParent": null,
"likes": {
"Likeid": null,
"dislikes": null,
"postid": null,
"userid": null
},
"comments": {
"commentsid": null,
"comments": null,
"Cpostid": null,
"time": null
}
}
];
I want my output like below:
{
"PostId": "1",
"PostVideo": "v2.mp4",
"PostDesc": "this is a tutorial on bench press",
"PostImage": "null",
"PostedBy": "Irfan khan",
"UserName": "irfan",
"Status": "1",
"PostParent": null,
"likes":
'0':{
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
'1':{
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
'2':{
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
'3':{
"Likeid": "1",
"dislikes": "0",
"postid": "1",
"userid": "1"
},
"comments":
'0':{
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
},
'1':{
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
},
'2':{
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
},
'3':{
"commentsid": "1",
"comments": "awesome video",
"Cpostid": "1",
"time": "2020-12-16 17:33:33"
}
},
{
"PostId": "9",
"PostVideo": null,
"PostDesc": "gdhgh",
"PostImage": "ghghdf",
"PostedBy": null,
"UserName": null,
"Status": null,
"PostParent": null,
"likes": {
"Likeid": null,
"dislikes": null,
"postid": null,
"userid": null
},
"comments": {
"commentsid": null,
"comments": null,
"Cpostid": null,
"time": null
}
}
Hmm... At first I thought that your loops were the problem, and you misunderstood the behavior. But then I saw your "i want my output like below", and that didn't made sense, specifically you wanting duplicate likes/comments.
First, the loops: a for loop and an inner foreach loop. The for loop iterates over the number of results. The inner foreach loop iterates over all the results. So if we have 6 results, instead of only iterating 6 times, we iterate 36 times (6 result count x 6 results).
We actually need only 1 loop:
$query = mysqli_query($mysqli, $sql);
$menus = [];
$rows = mysqli_fetch_all($query, MYSQLI_ASSOC);
foreach ($rows as $index => $row) {
$menus[] = [
...
];
}
Next, we avoid duplicate posts by indexing the $menus with PostId. If the PostId does not yet exist in $menus, we insert the post; otherwise we just append the like/comment:
$query = mysqli_query($mysqli, $sql);
$menus = [];
$rows = mysqli_fetch_all($query, MYSQLI_ASSOC);
foreach ($rows as $row) {
if (!isset($menus[$row['PostId']])) {
$menus[$row['PostId']] = [
'PostId' => $row['PostId'],
...
'comments' => [],
'likes' => [],
];
}
$menus[$row['PostId']]['comments'][] = [
...
];
$menus[$row['PostId']]['likes'][] = [
...
];
}
If for some reason you don't want the $menus indexed by PostId, we can just call array_values() after the loop to re-index it with zero-based sequential integer:
foreach (...) { ... }
$menus = array_values($menus);
As for the duplicate likes/comments, you were explicit about it. But just in case you actually want non-duplicate, we can also index them by their Likeid and commentsid, and insert/ignore as necessary:
$query = mysqli_query($mysqli, $sql);
$menus = [];
$rows = mysqli_fetch_all($query, MYSQLI_ASSOC);
foreach ($rows as $row) {
if (!isset($menus[$row['PostId']])) {
$menus[$row['PostId']] = [
'PostId' => $row['PostId'],
...
'comments' => [],
'likes' => [],
];
}
if (!isset($menus[$row['PostId']]['comments'][$row['commentsid']])) {
$menus[$row['PostId']]['comments'][$row['commentsid']] = [
...
];
}
if (!isset($menus[$row['PostId']]['Likes'][$row['Lid']])) {
$menus[$row['PostId']]['Likes'][$row['Lid']] = [
...
];
}
}
And if we also don't want the comments/likes indexed by commentsid/Lid, we can call array_values() on them as well:
foreach (...) { ... }
$menus = array_values($menus);
foreach ($menus as &$menu) {
$menu['comments'] = array_values($menu['comments']);
$menu['likes'] = array_values($menu['likes']);
}
unset($menu);
// Or
array_walk($menus, function (&$menu) {
$menu['comments'] = array_values($menu['comments']);
$menu['likes'] = array_values($menu['likes']);
});

How to get json output in php from url and get specific result using foreach

First of all I don't really know how to get the result of json which is so much complicated like this:
{
"SearchTime": 190,
"TotalTime": 190,
"SuggestedResponseType": "Person",
"ResultList": [
{
"__type": "ContactResult",
"TotalCount": 57079,
"Type": "Person",
"Contacts": [
{
"UnitId": 459551,
"Id": "459551S1",
"Name": "John d",
"VisitationAddress": "Lauvha 7, 87820",
"PostAddress": null,
"Value": "74 00 00 00",
"ValueType": "Phone",
"Distance": null,
"Rank": 0,
"BusinessName": null,
"Coordinate": {
"Lat": 64.4595741,
"Lon": 11.55081
},
"Logo": null,
"ContactPoints": [
{
"DisplayValue": "911 00 000",
"Indent": 2,
"Label": "Mobiltelefon",
"MainInfo": false,
"Type": "MobilePhone",
"Value": "91000000"
}
],
"FirstName": null,
"LastName": null,
"OrganizationNumber": null,
"Address": {
"PostAddress": null,
"StreetAddress": {
"CityArea": null,
"Coordinate": {
"Lat": 94.4595741,
"Lon": 71.55081
},
"County": "Tr\u00c3\u00b8ndelag",
"DisplayValue": "Lauvha 7 87820 Spilm",
"Entrance": "",
"HouseNumber": "7",
"Houses": null,
"Municipal": "Namsos",
"PostArea": "Spilm",
"PostCode": "87820",
"Street": "Lauvha"
}
},
"Products": null,
"ProfilePictures": null,
"ContactType": "Person"
},
{
"UnitId": 0,
"Id": "Footer",
"Name": "Vis flere treff",
"VisitationAddress": null,
"PostAddress": null,
"Value": "{\"q\":\"john\",\"type\":\"p\",\"start\":1,\"page\":2}",
"ValueType": null,
"Distance": null,
"Rank": 0,
"BusinessName": null,
"Coordinate": null,
"Logo": null,
"ContactPoints": null,
"FirstName": null,
"LastName": null,
"OrganizationNumber": null,
"Address": null,
"Products": null,
"ProfilePictures": null,
"ContactType": null
}
]
}
],
"Stunts": null
}
Next: How can i get the output: name, VisitationAddress and value from (ContactPoints)to be in correct format using foreach
and rid the
"Name": "Vis flere treff",
"Value": "{\"q\":\"john\",\"type\":\"p\",\"start\":1,\"page\":2}",
in the last line.
You'll need to read up on how to interact with objects and arrays in PHP.
$object->property, $array['key'], $object->array_property[$index]->key, etc.
For your particular case, you need to get your JSON using cURL or file_get_contents() or whatever other method you want. Then you need to decode the response with json_decode().
Once you have your JSON object it's just a matter of checking to see if values exist and outputting them however you want. Notice there are a bunch of arrays inside objects, but something like the following will get you started
<?php
$response = file_get_contents( 'https://zabihullah.com/veg/example.json' );
//$response = '{"SearchTime":190,"TotalTime":190,"SuggestedResponseType":"Person","ResultList":[{"__type":"ContactResult","TotalCount":57079,"Type":"Person","Contacts":[{"UnitId":459551,"Id":"459551S1","Name":"John d","VisitationAddress":"Lauvha 7, 87820","PostAddress":null,"Value":"74 00 00 00","ValueType":"Phone","Distance":null,"Rank":0,"BusinessName":null,"Coordinate":{"Lat":64.4595741,"Lon":11.55081},"Logo":null,"ContactPoints":[{"DisplayValue":"911 00 000","Indent":2,"Label":"Mobiltelefon","MainInfo":false,"Type":"MobilePhone","Value":"91000000"}],"FirstName":null,"LastName":null,"OrganizationNumber":null,"Address":{"PostAddress":null,"StreetAddress":{"CityArea":null,"Coordinate":{"Lat":94.4595741,"Lon":71.55081},"County":"Trøndelag","DisplayValue":"Lauvha 7 87820 Spilm","Entrance":"","HouseNumber":"7","Houses":null,"Municipal":"Namsos","PostArea":"Spilm","PostCode":"87820","Street":"Lauvha"}},"Products":null,"ProfilePictures":null,"ContactType":"Person"},{"UnitId":0,"Id":"Footer","Name":"Vis flere treff","VisitationAddress":null,"PostAddress":null,"Value":"{\"q\":\"john\",\"type\":\"p\",\"start\":1,\"page\":2}","ValueType":null,"Distance":null,"Rank":0,"BusinessName":null,"Coordinate":null,"Logo":null,"ContactPoints":null,"FirstName":null,"LastName":null,"OrganizationNumber":null,"Address":null,"Products":null,"ProfilePictures":null,"ContactType":null}]}],"Stunts":null}';
$json = json_decode( $response );
foreach( $json->ResultList[0]->Contacts as $contact ){
//var_dump( $contact );
if( $contact->Name == 'Vis flere treff' ) continue;
if( !empty( $contact->Name ) ) echo "Name: {$contact->Name}\r\n";
if( !empty( $contact->VisitationAddress ) ) echo "Address: {$contact->VisitationAddress}\r\n";
if( !empty( $contact->Value ) ) echo "Value: ". str_replace(' ', '', $contact->Value) ."\r\n";
echo "\r\n";
}
The result of that is the following:
Name: John d
Address: Lauvha 7, 87820
Value: 91000000
Name: Vis flere treff
You can see an example here: http://sandbox.onlinephpfunctions.com/code/ad6aa7fffaf42a32cb8f5568acb24c157c612467

Getting information from nested arrays php

I am making calls to an API
I have the response as an associative array so that I can use:
$field = $response['nameOfKey'];
However, some of the values for keys are themselves arrays like the following:
{
"Title": "Mr",
"Forenames": "Steve",
"Surname": "Williams",
"CountryOfBirth": 1,
"EmailAddress": "john.doe#email.com",
"EmailType": "Personal",
"BirthDate": "\/Date(632880000000)\/",
"Suffix": null,
"NationalInsuranceNumber": null,
"PrimaryAddress": {
"Address1": "Flat 1",
"Address2": "Oxford Street",
"City": "London",
"County": "London",
"Postcode": "L12456",
"Country": 1
},
"AdditionalAddresses": [
{
"Address1": null,
"Address2": null,
"City": null,
"County": null,
"Postcode": null,
"Country": 0,
"AddressType": 0
}
],
"PrimaryTelephone": {
"Number": "123456789",
"DialingCode": 1,
"TelephoneType": 1
},
"AdditionalTelephone": [
{
"Number": "223456789",
"DialingCode": 2,
"TelephoneType": 2
}
],
"BankAccount": {
"AccountName": "John Doe Account",
"AccountNumber": "123456789",
"SortCode": "123456"
},
"PrimaryCitizenship": {
"CountryOfResidency": 1,
"TaxIdentificationNumber": "AB12CD34EF56"
},
"AdditionalCitizenship": [
{
"CountryOfResidency": 0,
"TaxIdentificationNumber": null
}
],
"ExternalCustomerId": "91",
"ExternalPlanId": "91",
"PlanType": 10
}
So at the moment to get Forename I can just do $forename = $decodedResponse["Forenames"]; but I'm quite baffled at trying to get values from the inner arrays.
I thought I could do something like this:
foreach($decodedResponse as $data)
{
foreach($data['TelephoneNumbers'] as $tel)
{
echo $tel['Number']; die();
}
}
Essentially loop through the original Associative array and then loop through a specific key to get its values.
Your should use foreach for following array items: AdditionalAddresses, AdditionalTelephone and AdditionalCitizenship. Otherwise just chain array keys. See examples:
$forename = $decodedResponse['Forenames'];
$address2 = $decodedResponse['PrimaryAddress']['Address2'];
foreach ($decodedResponse['AdditionalTelephone'] as $tel) {
echo $tel['Number'];
}

How do I create a JSON-encoded 2D array?

I'm trying to create a two-dimensional array with the following code, but it gets JSON-encoded as an object. How do I fix this?
$result = $bd->query("select * from contenidos where idfolleto=$idf order by fila");
$arr = array();
if ($result) {
while ($row = mysqli_fetch_object($result)) {
$filaAct = $row->fila;
$arr[$filaAct][] = (array) $row;
}
}
echo json_encode($arr);
The output is:
{
"1": [{
"id": "6",
"idfolleto": "1",
"fila": "1",
"orden": "1",
"tipo": "carrousel",
"titulo": "",
"subtitulo": null,
"color1": null,
"color2": null,
"color_fondo": null
}],
"2": [{
"id": "7",
"idfolleto": "1",
"fila": "2",
"orden": "1",
"tipo": "texto-imagenes",
"titulo": "Texto 1",
"subtitulo": null,
"color1": null,
"color2": null,
"color_fondo": null
}, {
"id": "8",
"idfolleto": "1",
"fila": "2",
"orden": "2",
"tipo": "texto-imagenes",
"titulo": "Texto 2",
"subtitulo": null,
"color1": null,
"color2": null,
"color_fondo": null
}],
"3": [{
"id": "9",
"idfolleto": "1",
"fila": "3",
"orden": "3",
"tipo": "texto-imagenes",
"titulo": "Texto 3",
"subtitulo": null,
"color1": null,
"color2": null,
"color_fondo": null
}]
}
You would have to index it without using your own defined indices.
If you must maintain using fila as the indices then you have to understand that the encoded JSON will be an object. You can json_decode it back into an array though like: json_decode($json, 1);
Otherwise before encoding the array you could call: $arr = array_values($arr); which would result in the array being encoded as a JSON array once you call json_encode.
If you're sure that your fila fields will contain "indexes" (I mean continuous integers starting from 0), then you can convert them to integers using intval.
if ($result) {
while ($row = mysqli_fetch_object($result)) {
$arr[intval($row->fila)][] = (array) $row;
}
}
Otherwise, it probably would be better to just group the records and create an array of the groups.
if ($result) {
$groups = array();
while ($row = mysqli_fetch_object($result)) {
$groups[$row->fila][] = (array) $row;
}
$arr = array_values($groups);
}

Unknown nested level array/json dispaly in HTML/PHP

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

Categories