Search code examples
phparraysjsoncodeigniterassociative-array

How to produce a line space in arrays


$arr = array(
   'toemail'=>$v->agent_primary_email,
   'agentname'=>$v->agent_firstname,
   'agentid'=>$v->agent_id,
   'subject'=>'The details of total number of properties saved by your clients',
   'totalprop'=>$v->prop_count
);
echo json_encode($arr);exit;

The output looks like this

{"toemail":"[email protected]","agentname":"john","agentid":"110012","subject":"The    details of total number of properties saved by your clients","totalprop":"131"}

But what changes should i have make, so that the output looks like this

{"toemail":"[email protected]",
 "agentname":"john",
 "agentid":"110012",
 "subject":"The details of total number of properties saved by your                     clients",
 "totalprop":"131"}

Solution

  • Use JSON_PRETTY_PRINT and also need to use echo "<pre>";

    From PHP Manual: Use whitespace in returned data to format it. Available since PHP 5.4.0

    $array = array(
        'test'=>1,
        'test2'=>'test',
        'test3'=>'test 3'
    );
    echo "<pre>";
    echo json_encode($array,JSON_PRETTY_PRINT);
    

    Result:

    {
        "test": 1,
        "test2": "test",
        "test3": "test 3"
    }