0

I am new! please go easy on me.

I have a PHP file that when run, sends an email. The contents of the email is a long json string, but for example, lets say that it's a short string like this:

{
  "firstName": "Jon",
  "lastName": "Ryan"
}

The string is held in a variable called $json_response.

In terms of research, I found a .js library that outputs json exactly in the way that I'm looking for. It's here https://github.com/marianoguerra/json.human.js ... but I don't know how to use that library properly in my project.

So, I'm wondering if it's possible to get an output similar the output of the json.human.js library, but using PHP rather than js?

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Jonirish
  • 49
  • 6
  • Read this comment only if you're not lazy, you can use `json_decode` and build a customized function to iterate the object and print it according to your needs. – Ofir Baruch May 07 '15 at 09:15

4 Answers4

1

You will the json extension for PHP.

Then you can do:

$json_array = json_decode($json_response, true);
echo $json_repsonse["firstName"];
echo $json_repsonse["lastName"];
Ryan
  • 164
  • 1
  • 6
  • Thanks for the help. I see what you're doing there. If I echoed the values for firstname, lastname etc sequentially that would be fine. But I need to transfer all the outputs then to email. So I really need to be able to output all the values at once. I think. Look, thanks for pitching in. – Jonirish May 07 '15 at 09:57
  • @DoubleGlick Then you can do: echo "Hello {$json_response['firstName']} {$json_response['lastName']}, how're you"; – Ryan May 08 '15 at 08:24
0

You should take a look at Krumo

http://krumo.sourceforge.net/

Krumo basically turns arrays and objects into easily readable formats, but since you have json, you will need to use json_decode to turn it into an array, then Krumo it out.

Personally I use it on every project I work on because of how useful it is.

Hope this helps!

Community
  • 1
  • 1
Duenna
  • 2,186
  • 2
  • 14
  • 17
0

Use

json_decode('{ "firstName": "Jon", "lastName": "Ryan" }',true);

Then you will get an array as output,

Array
(
    [firstName] => Jon
    [lastName] => Ryan
) 

Then you can make use of this array in any way you want.

  • Thanks but the values in my json will be different each time so I don't believe that will work for me. – Jonirish May 07 '15 at 09:49
0

You can create Json to html table from this link Json-to-HTML-Table

Varun Naharia
  • 5,318
  • 10
  • 50
  • 84
  • While that's js (which I'm useless at), it seems to be a much simpler js than the one I mentioned in my question. So I'll give it a go. Thanks ! (if I can't get it to work, I'll probably need to " build a customized function to iterate the object and print it according to your needs"... as per Ofir Baruch's comment. Thank you. – Jonirish May 07 '15 at 10:01
  • You asked for json to human readable format and I think it will do the same work – Varun Naharia May 07 '15 at 10:08