Joined October 2012


Mobile & Web Developer at Brilliant Notion
Mobile & Web Developer
Brilliant Notion

Posted to Format json output over 1 year ago

Or you could just use JSON_PRETTY_PRINT:

$a = array(
    'foo' => 'bar',
    'boo' => 'far'

echo json_encode($a, JSON_PRETTY_PRINT);

Which will produce the following output:

    "foo": "bar",
    "boo": "far"

@jverdeyen That's one I haven't seen before. I like it!

@nashio Of course it is a lot. The delay is to accentuate the performance difference. I would never have any delay at all in a real application.

@nashio I put together a quick benchmark to demonstrate. This test times two AJAX calls in parallel (deferred) and in sequence (nested). They both use the same AJAX callback, which is a simple PHP script that has a 1s delay, then returns a random number.

When multiple AJAX calls are used with a jQuery deferred, isn't it correct that they will run in parallel? In this case, your example is not only easier to read, but has a performance boost. The only disadvantage that should be noted is that if there is a data dependency for the second request, based on the result of the first request, then they need to be nested so that they execute in sequence rather than in parallel.

@thomaspuppe This "protip" showed up in my e-mail box and I read it with horror. Thank God your correction is the top comment.

132 Karma
3,685 Total ProTip Views