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
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.
To use this function, you first declare you 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.
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:
Then $flat will look like:
and if you wanted printed in a nice html table: