-1

Hello I am using a for loop to prepare a JSON String

//prepare a JSON Array [ obj1,obj2,obj3.... ]

echo "[";

foreach ($usernames as $value){
    //prepare a JSON String for $username
    //e.g. {"username":$value}

    // insert comma if there is next element in the array
    echo ",";
}

echo "]";

My problem is, having a trailing comma is incorrect format in JSON. How do I determine the for loop has reached the end of the array, preparing the last element, and thus stops it from adding all but the last comma character?

Thankyou

Gapton
  • 1,932
  • 2
  • 19
  • 33
  • 2
    are you aware of [json_encode()](http://php.net/manual/en/function.json-encode.php)? – Brian Glaz Sep 19 '11 at 02:12
  • 1
    Build an array and send it through `json_encode()` instead of forming the json string manually. – evan Sep 19 '11 at 02:13
  • i am aware of it but somehow i completly forgot about it, oh my.... thank you for reminding me to implement the function in a more proper way. – Gapton Sep 19 '11 at 02:44

4 Answers4

5

Is there a reason you aren't using json_encode to do this? You can convert a normal php array to JSON with one quick call using json_encode.

$arr = array("a"=>"one", "b"=>"two");

echo $json_encode($arr);

http://php.net/manual/en/function.json-encode.php

Trevor
  • 10,791
  • 2
  • 31
  • 40
  • I was retrieving data from a DB so I got stuck in the mindset of manipulating strings. As suggested I have implemented it so that it now builds an associative array as a object, and stores this object in an array. I then call json_encode to encode that array. The code is clean and tidy, thanks. – Gapton Sep 19 '11 at 03:35
  • Sure thing. I thought you might be creating the JSON yourself for some other purpose. :) – Trevor Sep 19 '11 at 12:18
  • Why has this snippet with a glaring typo gained 5 upvotes? – mickmackusa Sep 05 '19 at 10:49
  • @mickmackusa perhaps you could point out the typo or fix it. I don't see it. – Trevor Dec 25 '19 at 20:45
  • https://3v4l.org/NJlvH I'll give a moment to try to nut it out yourself. – mickmackusa Dec 25 '19 at 20:47
3

If you must do it with a for, then change the printing of the comma to the beginning. Only print it if its not the first iteration.

K-ballo
  • 78,684
  • 20
  • 152
  • 166
  • No one should be recommending the hacking together of json strings. Important: `json_encode() not only offers the cleanest technique, it escapes as well when necessary. – mickmackusa Dec 25 '19 at 20:54
1

Well, you could store all of it in a variable, then after it is done you could just do:

$jsonVar = substr($jsonVar, 0, -1);

Before you add the last "]"

Jason Dean
  • 9,485
  • 26
  • 35
  • 2
    I like trevor's answer better. :) – Jason Dean Sep 19 '11 at 02:14
  • 1
    And what if he has 0 users? it will become `]` instead of `[]` – Daniel Sep 19 '11 at 02:15
  • Then you do `echo $json_encode((object)$arr);` – awm Sep 19 '11 at 02:18
  • Thanks for this quick solution. I ended up building them in php objects and encode them using json_encode because it is more tidy. But its nice to know this string operation which i never used before (i come from Java and C# C++ , only had 1 day in PHP ) – Gapton Sep 19 '11 at 03:37
  • No one should be recommending the hacking together of json strings. Important: `json_encode() not only offers the cleanest technique, it escapes as well when necessary. – mickmackusa Dec 25 '19 at 20:54
0

Solution specific to your problem

echo "[";
ob_start();
foreach ($usernames as $value){
    //prepare a JSON String for $username
    //e.g. {"username":$value}

    // insert comma if there is next element in the array
    echo ",";
}
$data = ob_get_contents();
ob_end_clean() ;
echo rtrim($data,',');
echo "]"

but correct / standard / easier way is to simply do json_encode($usernames);

Mr Coder
  • 8,053
  • 5
  • 41
  • 74
  • No one should be recommending the hacking together of json strings. Important: `json_encode() not only offers the cleanest technique, it escapes as well when necessary. – mickmackusa Dec 25 '19 at 20:55