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