Gary
Gary

Reputation: 3

How to add linebreak to decoded json array in php

I need to add some sort of line break after each segment (date, name, address, phone) to separate them, I have tried using the break and paragraph etc but just keep on getting syntax errors with the usual unexpected "<" and have also tried using tables but that failed too.

Is there an easy way to format the output of this to display each part on a separate line please?

<?php
    $file = file_get_contents('http://somesite.com/Data/jsontxt');
        $out = (json_decode($file));
            echo $out->date;
            echo $out->name;
            echo $out->address;
            echo $out->phone;
?>

This is a sample of the array:

{
"name":"Joe Bloggs",
"date":"11:58pm 28 August 2010",
"address":"13 Boggy Ave, Hamilton",
"phone":"555-5478"
}

and what I am currently getting:

Joe Bloggs11:58pm 28 August 201013 Boggy Ave, Hamilton555-5478

Upvotes: 0

Views: 1237

Answers (2)

shamittomar
shamittomar

Reputation: 46692

Use this code:

<?php
    $file = file_get_contents('http://somesite.com/Data/jsontxt');
    $out = (json_decode($file));

    echo $out->date    . '<br/>';
    echo $out->name    . '<br/>';
    echo $out->address . '<br/>';
    echo $out->phone   . '<br/>';
?>

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

You have to break the PHP block in order to output.

<?php
  foo();
?>
All mimsy were the borogoves, and the mome raths outgrabe.
<?php
  bar();
?>

Upvotes: 1

Related Questions