T
T
Tenebrius2015-02-23 13:53:32
PHP
Tenebrius, 2015-02-23 13:53:32

How to output JSON nicely?

There is a list of employees. You need to output it to JSON.
I do it like this:

$table=array();	 // создаю пустой массив
...
// тут в цикле читаю таблицу сотрудников
$row=array('worker_id'=>$p_id, 'status'=>$stat); // на каждом шаге создаю массив для отдельного сотрудника
$table[]=$row; // добавляю полученные данные в основной массив
// шаг цикла заканчивается

//в конце вывожу данные следующим образом
echo "<pre>".json_encode($table)."</pre>";

But I don't like how the output looks. Haven't worked with JSON before. The only example I know of is the answer to a Google captcha:
{
  "success": true|false,
  "error-codes": [...]   // optional
}

How to make an array output in a similar way (with hyphens after commas) and at the same time be readable for scripts as a JSON object?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Viktor Vsk, 2015-02-23
@Tenebrius

$json_string = json_encode($data, JSON_PRETTY_PRINT);

Source
Manual

I
Ivan Rakovnikov, 2015-02-23
@Shkolnik97

<pre>
<?php print_r(json_decode(json_encode($table), true)); ?>
</pre>

N
Nastya Sukharik, 2015-02-23
@nastya_cyxarik

json_decode on the client side and parse.

K
Kuroi, 2015-02-27
@Kuroi

What are you teaching people?
Install the JSONView browser extension.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question