0

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.

3
  • 2
    Would you be able to post the relevant sections of code? Commented Jan 3, 2013 at 3:24
  • Well you have to parse the xml with DOMDocument or similar and for json json_decode is enough. Commented Jan 3, 2013 at 3:27
  • If you want to see the print_r() in a nicer format you can view the HTML source in the browser, as the browser will render ignoring whitespace formatting so it will all show up in a giant blob of text. Or you can do this echo '<pre>' . print_r($your_decoded_json) . '</pre>'; Commented Jan 3, 2013 at 3:35

2 Answers 2

2

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>";
Sign up to request clarification or add additional context in comments.

1 Comment

OP has said they used print_r() already, which is far more readable than var_dump()
0

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.

1 Comment

What I meant is, I am receiving XML data and so I do not know the fields. In that way, how do I display the XML data?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.