I have the code:

$VolunS = array('aro','clergy','intskills','interpt','bus','child','law','cpr','cpryn','data','security','emergency','computer','mechanical','administration','firstaid','faid','translation','translate','construction','basicclean','foodprep','animalcare','heavy','other3');
foreach($VolunS as $skill){
    if($person["$skill"] != ""){
        echo $person["$skill"] . ", "; //this is the line where echo is replacing $row .=
    }
}

And my problem is that I am adding an extra comma at the end of the list. The purpose of this code is to write to a variable named $row which is taken out and replaced with echo so that I am able to see the output easier. When the list is echoed I get exactly what I want BUT it adds that comma at the end. I know why it is doing that, it's how the code is written, but how do I make it so that it doesn't do that. I did it before with an array that was much smaller, it contained 4 variabless to write so all I had to do was write 8 or so if statements that will check if it isset() and then checks to make sure one of the variables after that variable is set, if not it doesn't add the comma, if there exsists an element in the set that contains some value, then it adds the comma because it knows that it will come upon a case to which it will write another viarble which needs to be seperated with a space.

I don't know if I am being clear enough, so let me write this breif summary below.

The code above works but adds the extra comma because the line: echo $person["$skill"] . ", "; has the part at the end where it adds a comma then space. If I take that part out it will not add a comma and space at all. Is there a way using Implode that it can be done? Or any other way you can think of?

Example of output:

Amateur Radio Operator, Child Care, CPR, Yes, First Aid, Basic Clean-up Skills, Animal Care/Rescue, Heavy Equipment Operation, Child Care, CPR, Yes, Data Entry, First Aid, Animal Care/Rescue, 

at the end you can see an extra coma after Animal Care/Rescue while no element follows in the list. That is exactly what I am trying to fix.

UPDATE:

I found a method that seems to work but I don't know if it is the best way to get this working. Updated code below:

$VolunS = array('aro','clergy','intskills','interpt','bus','child','law','cpr','cpryn','data','security','emergency','computer','mechanical','administration','firstaid','faid','translation','translate','construction','basicclean','foodprep','animalcare','heavy','other3');  
        $i = 1;
        foreach($VolunS as $skill){
            if($person["$skill"] != ""){
                if($i == 1){
                    echo $person["$skill"];
                    $i++;
                }else {
                    echo ", " . $person["$skill"];
                }
            }
        }

Well, this join() function should help your life a lot easier if you are dealing with printing out an array to string with delimiter characters ;)

Member Avatar for diafol

join() works as does implode()

$string = implode(",",$VolunS);
echo $string;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.