I have this json data
{
"next_offset": -1,
"records": [
{
"id": "87dad127-a60e-15a3-148e-56c7675f11df",
"name": "1A Unit",
"suite": "1A",
"sf": 1200,
"unit_floor": 1,
},
{
"id": "f386a7ad-d6ea-6eb1-9b8f-56cd6104c445",
"name": "3B Unit",
"suite": "3B",
"sf": 450,
"unit_floor": 3,
},
{
"id": "b352dc84-8c76-6c27-daa4-56cd61e71aa2",
"name": "3A Unit",
"suite": "3A",
"sf": 450,
"unit_floor": 3,
},
{
"id": "8ec4325a-1271-560e-888b-56cd6120f5de",
"name": "2B Unit",
"suite": "2B",
"sf": 450,
"unit_floor": 2,
},
{
"id": "5a15fd5e-246a-be4b-fd5d-56cd619e9ee1",
"name": "1B Unit",
"suite": "1B",
"sf": 450,
"unit_floor": 1,
},
{
"id": "61a55092-5683-1088-2d6c-56c99c5d4873",
"name": "2A Unit",
"suite": "2A",
"sf": 3000,
"unit_floor": 2,
}
]
}
I want to display this data in table depending on the floor number,. Like 3rd floor units 3A, 3B and total of their area in sqft in one row, 2nd floor units 2A, 2B and their sum of area in one row and 1st floor units 1A, 1B and their area sum in one row. Please help me.
I tried this but its not giving the expected result
$unit_response = call($url, $oauth2_token_response->access_token, 'GET');
$unit_json = json_decode(json_encode($unit_response),true);
<table class="stak-plan">
<?php foreach ($unit_json['records'] as $unit_data) { ?>
<tr>
<td>Floor: 3</td>
<td>
Suit : <?php echo $unit_data['name'] ?><br>
SF : <?php echo $unit_data['sf'] ?>
</td>
<td>Suite: 3B<br /> SF: 25,000</td>
<td>Suite: 3C<br /> SF: 25,000</td>
<td>
</td>
</tr>
<?php } ?>
</table>
CSS:
<style>
td{
border: 1px solid black;
padding: 15px;
}
</style>
JSON (some extra commas has been removed):
$json = '{
"next_offset": -1,
"records": [
{
"id": "87dad127-a60e-15a3-148e-56c7675f11df",
"name": "1A Unit",
"suite": "1A",
"sf": 1200,
"unit_floor": 1
},
{
"id": "f386a7ad-d6ea-6eb1-9b8f-56cd6104c445",
"name": "3B Unit",
"suite": "3B",
"sf": 450,
"unit_floor": 3
},
{
"id": "b352dc84-8c76-6c27-daa4-56cd61e71aa2",
"name": "3A Unit",
"suite": "3A",
"sf": 450,
"unit_floor": 3
},
{
"id": "8ec4325a-1271-560e-888b-56cd6120f5de",
"name": "2B Unit",
"suite": "2B",
"sf": 450,
"unit_floor": 2
},
{
"id": "5a15fd5e-246a-be4b-fd5d-56cd619e9ee1",
"name": "1B Unit",
"suite": "1B",
"sf": 450,
"unit_floor": 1
},
{
"id": "61a55092-5683-1088-2d6c-56c99c5d4873",
"name": "2A Unit",
"suite": "2A",
"sf": 3000,
"unit_floor": 2
}
]
}';
PHP:
$jd = json_decode($json);
$floors = array();
foreach ($jd->records AS $key => $obj) {
$f = $obj->unit_floor;
$id = $obj->id;
$name = $obj->name;
$suite = $obj->suite;
$sf = $obj->sf;
$floors[$f][$suite]['name'] = $name;
$floors[$f][$suite]['id'] = $id;
$floors[$f][$suite]['sf'] = $sf;
}
//sort floors in desc order
krsort($floors);
foreach($floors as $id => $floor){
ksort($floors[$id]);
}
print '<table>';
foreach($floors as $floor => $suites){
$sqf = 0;
print '<tr>';
print '<td>FLOOR: '.$floor.'</td>';
foreach($suites AS $suite => $value){
$sqf += $value['sf'];
print '<td>Suite: '.$suite.'<br>SF: '.$value['sf'].'</td>';
}
print '<td>'.$sqf.'</td>';
print '</tr>';
}
print '</table>';
OUTPUT:
To achieve this in PHP, load the file from somewhere using read() and then execute json_decode()
in its contents, effectively creating an array, then use a foreach
loop to iterate through each, using a template to display the values. Also remember to pass the second argument as it creates an associative array instead of an object.
$contents = //open file from fs or wherever you want to get it (curl, etc)
$array = json_decode($contents, 1); //argument for assoc array
foreach ($array as $key => $value) {
// echo your table...
}
This is a rough idea of how to do it, but should get you on your way.
A more complete idea would be for example,
<table>
<?php foreach ($array as $key => $val): ?>
<!-- your table arrangement in html -->
<?= $key ?>
<?php // do whatever you want here with the array... ?>
<?= $value ?>
<?php endforeach; ?>
</table>
EDIT: I see your error, you're passing a key to a foreach loop but foreach loops take arrays as input, unless the array is inside the key. If so, I'd need to see where you're decoding and where you're dumping it.
Read more about json_decode here: http://php.net/manual/en/function.json-decode.php
And here is an example of the argument in action: https://eval.in/525451
$json_string = "your json string like above";
//or you can $json_string = file_get_contents(url_return_your_json);
Then, you using json_decode
for convert it to array
$your_array = json_decode($json_string,1);
The problem seems to be your JSON format. You have "trailing commas" that will prevent it from being decoded properly, and thus, getting empty results in your foreach
loop. You have two options:
fix your JSON and remove the trailing commas
{
"next_offset": -1,
"records": [
{
"id": "87dad127-a60e-15a3-148e-56c7675f11df",
"name": "1A Unit",
"suite": "1A",
"sf": 1200,
"unit_floor": 1, // <-- TRAILING COMMA
}
]
}
Use a function (source) that automatically removes these commas before you run your json_decode
:
function removeTrailingCommas($json)
{
$json = preg_replace('/,\s*([\]}])/m', '$1', $json);
return $json;
}
$json = ''; // <-- your ogiginal JSON string
$json = removeTrailingCommas($json);
$unit_json = json_decode($json, 1);
Once you do that, you can then iterate in this manner to create your table:
$floors = array();
foreach ($unit_json['records'] as $record) {
$floors[$record['unit_floor']][$record['suite']] = $record;
}
krsort($floors);
?>
<table class="stak-plan">
<?php foreach ($floors as $floor_num => $floor_units) { ?>
<tr>
<td>Floor: <?= $floor_num ?></td>
<?php
ksort($floor_units);
foreach ($floor_units as $unit) {
?>
<td>
Suite : <?= $unit['suite'] ?><br>
SF : <?= $unit['sf'] ?>
</td>
<?php
}
?>
<td>
<?php
echo array_sum(array_map(function ($item) {
return $item['sf'];
}, $floor_units));
?>
</td>
</tr>
<?php } ?>
</table>
this is other way to get the solution :
<?php
$json = '{
"next_offset": -1,
"records": [
{
"id": "87dad127-a60e-15a3-148e-56c7675f11df",
"name": "1A Unit",
"suite": "1A",
"sf": 1200,
"unit_floor": 1
},
{
"id": "f386a7ad-d6ea-6eb1-9b8f-56cd6104c445",
"name": "3B Unit",
"suite": "3B",
"sf": 450,
"unit_floor": 3
},
{
"id": "b352dc84-8c76-6c27-daa4-56cd61e71aa2",
"name": "3A Unit",
"suite": "3A",
"sf": 450,
"unit_floor": 3
},
{
"id": "8ec4325a-1271-560e-888b-56cd6120f5de",
"name": "2B Unit",
"suite": "2B",
"sf": 450,
"unit_floor": 2
},
{
"id": "5a15fd5e-246a-be4b-fd5d-56cd619e9ee1",
"name": "1B Unit",
"suite": "1B",
"sf": 450,
"unit_floor": 1
},
{
"id": "61a55092-5683-1088-2d6c-56c99c5d4873",
"name": "2A Unit",
"suite": "2A",
"sf": 3000,
"unit_floor": 2
}
]
}';
function to parse data:
function createArray($unit_json){
$max_unit_floor = 0;
$subcells=array_map(function($n)use(&$max_unit_floor){
if((int)$max_unit_floor < (int)$n->unit_floor){ $max_unit_floor = $n->unit_floor; }
return preg_replace('/\d*/','',$n->suite);
},$unit_json->records);
$subcells = array_flip(array_flip($subcells));
$cells = array();
for($i = $max_unit_floor; $i > 0; $i--){
$values = array();
foreach($subcells as $v){
$values[] = $i.$v;
}
$cells['floor_'.$i] = array('title'=>'Floor: '.$i,'values'=>$values,"total"=>0);
}
foreach ($unit_json->records as $unit_data){
foreach ($cells as $key=>$value){
for($i=0;$i<count($value['values']);$i++)
if($value['values'][$i]==$unit_data->suite){
$cells[$key][$value['values'][$i]] = array("sf"=>$unit_data->sf);
$cells[$key]["total"] += (float)$unit_data->sf;
}
}
}
return $cells;
}
?>
html table:
<table class="stak-plan" border="1" cellpadding="4" cellspacing="0">
<?php
$unit_json = json_decode($json);
$cells = createArray($unit_json);
foreach ($cells as $key=>$value) {
?>
<tr>
<td><?php echo $value['title']?></td>
<?php foreach($value['values'] as $k=>$cell){ ?>
<td>Suite: <?php echo $cell?><br /> SF: <?php echo isset($cells[$key][$cell]['sf'])?$cells[$key][$cell]['sf']:''; ?></td>
<?php }?>
<td>sum: <?php echo $cells[$key]["total"]; ?></td>
</tr>
<?php } ?>
</table>
response: