I am making use of an available API to send requests and receive the results which is in json and xml formats. I am able to do so but how do I display the returned data properly?
I used json_decode and assign the xml data to an array and print_r those. It shows up in a huge junk of data.
How do I show it on a table form? Or must I save the data into individual files or database first before displaying?
I am new to PHP, thus not sure how to implement this.
JSON is a multidimensional-array, the easiest way to view it, in my opinion, is to just
var_dump($json_array);
Though, this may be the chunk of data you're referring to.
It's not a very table friendly format, as a table is inherently 2-dimensional, and JSON can be many-dimensional.
You can flatten the array, and then display it as a table.
function flatten_json($json, &$flat, $key) {
// First check the base case: if the arg is not an array,
// then it's data to be inserted in the flat array.
if (!is_array($json)) {
$flat[$key] = $json;
} else {
// It's an array, to reduce the depth of the json,
// we remove this array, by iterating through it,
// sending the elements to be checked if they're also arrays,
// or if they're data to be inserted into the flat array.
foreach ($json as $name => $element) {
flatten_json($element, $flat, $key.'_'.$name);
}
}
}
To use this function, you first declare you flat array:
$flat = array();
Then pass it to the function, with your json, and a key you want to be the outer key, if you are sure that your json is an array, which is pretty much guaranteed, you can leave the key empty.
flatten_json($json_array, $flat, '');
Now $flat will have the flattened json, and you can print it as a table, maybe into a csv if you have many json results to print.
If your json was:
array(
'person' => array(
'name' => 'timmy',
'age' => '5'
),
'car' => array(
'make' => 'ford',
'engine' => array(
'hp' => 260,
'cyls' => 8
)
)
)
Then $flat will look like:
array(
'person_name' => 'timmy',
'person_age' => 5,
'car_make' => 'ford',
'car_engine_hp' => 260,
'car_engine_cyls' => 8
)
and if you wanted printed in a nice html table:
echo "<table><tr>";
foreach ($flat as $header => $value) {
echo "<th>$header</th>;
}
echo "</tr><tr>";
foreach ($flat as $header => $value) {
echo "<td>$value</td>";
}
echo "</tr></table>";
I am slight confuse. but if you're referring to an xml file that will appear somethign like this :
<books>
<book published="2011-07-24 19:40:26">
<title>I left my heart on Europa</title>
<author>Ship of Nomads</author>
< /book>
<book published="2011-07-24 19:40:26">
<title>I left my liveron Europa</title>
<author>Ship of Nomads</author>
< /book>
</books>
You can do some php trick here by using simplexml_load_file. Example from the above xml you can have this code :
$mybooks= simplexml_load_file('books.xml');
echo "<ul id="booklist">";
foreach ($mybooks as $bookinfo):
$title=$bookinfo->title;
$author=$bookinfo->author;
$date=$bookinfo['published'];
echo "<li><div class="title">".$title."</div><div class="author">by ".$author."</div><b>".$date."</b></li>";
endforeach; echo "</ul>";
Hope this helps.