Hi,
I hate to say this - but the process you've got is fundamentally flawed at the moment. It's all fine right up until you implode the sports array into one string. Why? because the sports array will only be populated by the sports they've actually ticked, right? So for one user you might have:
football,rugby,tennis
and for another you might have:
tennis,snooker
There is no correlation between the location of the data in the string and which checkbox it originally came from. The only way you could do it would be to create a checkbox who's name is the selected sport, something like:
for ($i=0; $i < count($sportsArray); $i++) {
echo "<input type=checkbox name={$sportsArray[$i]} checked>";
}
Then when they re-submit the form you just build a new sportsArray from what they submit (one's they un-check will get automatically removed).
However what I think you're after is that the user see's the same options again - but the ones they picked are ticked, right?
One (quite nasty imho) way of doing this is in the original form - don't use one Array for all the sports. Give each checkbox a unique name - and then check to see if it was ticked or not. If not place a 0 (or something) into the string, if it was place a 1. So it might look like:
1,0,0,0,1,1,0,1
(etc)
Then at least you can perform the reverse very easily - if there was a 1 then it was checked, otherwise leave it.
Personally I'd re-design this so that each sport was a unique field in the database, but hey - that's your choice 🙂
Cheers,
Rich