0

How do I make the final foreach instance end without the , character? As it stands every instance of $var2 is followed by a , character. Code to follow, thanks in advance.

foreach($tags as $var1 => $var2) {
    if($_SESSION[$var1]!="off")
    {
    include($var2);
    echo",";    
    //needs to include no , character on the last instance.
    }
    else
    {
    echo $var1;
    }
}
1
  • 2
    This question seems unclear to me. What are you exactly trying to do? Commented Feb 18, 2016 at 9:02

6 Answers 6

1

Inside your foreach, change the echo ","; to following:

if (end($tags) != $var2) {
  echo ",";
}

Doing this will check if you are in the last index by comparing the current index to the last index of the $tags array. Click here to see the PHP wiki on the end() function.

Sign up to request clarification or add additional context in comments.

Comments

0

Using this you retrieve the last element with end() and gets is key with key() that you compare with the current key:

foreach($tags as $var1 => $var2) {
    if($_SESSION[$var1]!="off")
    {
        include($var2);
        //needs to include no , character on the last instance.
        if(key(end($tags)) !== $var1)
        {
            echo",";
        }
    }
    else
    {
        echo $var1;
    }
}

Comments

0

This wil help you

 $result = '';
    foreach($tags as $var1 =>$var2) {
        $result .= $var2.',';
    }
    $result = rtrim($result,',');
    echo $result;

Comments

0

Try using the end() function to get the last item:

$last_item = end(array_keys($tags));
foreach($tags as $var1 => $var2) {
    if($_SESSION[$var1]!="off") {
    {
        include($var2);
        if($var1 !== $last_key) {
            echo",";    
        }
    //needs to include no , character on the last instance.
    }
    else
    {
        echo $var1;
    }
}

Comments

0

One way to check the last element and echo comma(,)

$numItems = count($tags );
$i = 0;
foreach($tags  as $var1 =>$var2) {
if($_SESSION[$var1]!="off")
{
include($var2);
if(++$i != $numItems) {
echo ",";
}
}
else
{
echo $var1;
}
}  

Comments

0

First insert all your echo's into string. Then delete final instance of "," with preg_replace function:

$str = preg_replace('/\,$/', '', $str);

Another way is with a counter:

$len = count($tags);  
$iter = 0;
foreach($tags as $var1 => $var2) {
        if($_SESSION[$var1]!="off")
        {
        include($var2);
        if($iter != $len-1)
          echo",";    
        //needs to include no , character on the last instance.
        }
        else
        {
        echo $var1;
        }
      $iter++;
    }

Another way with end() function

foreach($tags as $var1 => $var2) {
    if($_SESSION[$var1]!="off")
    {
    include($var2);
    if (end($tags) != $var2) {
       echo",";    
    //needs to include no , character on the last instance.
    }
    else
    {
    echo $var1;
    }
}

1 Comment

Although it works fine, preg_replace is a little on heavy for something like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.