2

How to add commas to a json print?

$result = curl($url);
$result = json_decode($result , true);

$resultdata = $result ['data'];
foreach($resultdata as $data){
$print= array(
"id" => $data['id'],
"username" => $data['username'],
"text" => $data['text']
);
print json_encode($print);                              
}

this is the response from my code

{
    "id": "17996292388215089",
    "username": "hanikfadhilah",
    "text": "Loh kapan ini huuu pengen"
}
{
    "id": "17877856039348099",
    "username": "titan_kdk",
    "text": "Mntb" 
}
{
    "id": "17860767967398064",
    "username": "explorecentraljava",
    "text": "Terbaik fotonya lur" 
}

I want to have a comma for each json result

{
    "id": "17996292388215089",
    "username": "hanikfadhilah",
    "text": "Loh kapan ini huuu pengen"
},{
    "id": "17877856039348099",
    "username": "titan_kdk",
    "text": "Mntb"
},{
    "id": "17860767967398064",
    "username": "explorecentraljava",
    "text": "Terbaik fotonya lur"
}
2
  • print json_encode($print) . ","; Commented May 27, 2019 at 4:21
  • @Rizky Nurichsan what do you want to achieve by adding comma? What do you need this for? Commented May 27, 2019 at 10:06

2 Answers 2

3

What you actually need to do is produce an array of results, which you can do by pushing values into an array in the loop, and then json_encode the array after the loop:

$print = array();
foreach($resultdata as $data){
    $print[]= array(
        "id" => $data['id'],
        "username" => $data['username'],
        "text" => $data['text']
    );
}
print json_encode($print); 
Sign up to request clarification or add additional context in comments.

Comments

0

I don't get the point of having ',' but I'm guessing you want a valid json output. If so I guess that your result data is an array:

<?php
$result = [ 'data' => [
            [
                "id" => "17996292388215089",
                "username" => "hanikfadhilah",
                "text" => "Loh kapan ini huuu pengen"
            ],
            [
                "id" => "17877856039348099",
                "username" => "titan_kdk",
                "text" => "Mntb"     
            ],
            [
                "id" => "17860767967398064",
                "username" => "explorecentraljava",
                "text" => "Terbaik fotonya lur"
            ]
        ]
    ];

so all what you need to do in order to get it as a valid json is:

print json_encode($result['data'], JSON_PRETTY_PRINT);

that produces output:

[
    {
        "id": "17996292388215089",
        "username": "hanikfadhilah",
        "text": "Loh kapan ini huuu pengen"
    },
    {
        "id": "17877856039348099",
        "username": "titan_kdk",
        "text": "Mntb"
    },
    {
        "id": "17860767967398064",
        "username": "explorecentraljava",
        "text": "Terbaik fotonya lur"
    }
]

no need for any foreach loop.

json_encode()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.