-2

I have a loop in php

foreach($Array as $key =>$value) 
 {
     $TotalLine .=  " Qualification =".$value." OR ";
 }
  echo $TotalLine;

Prints

  Qualification =1 OR Qualification =2 OR Qualification =3 OR Qualification =4 OR

Now i need to remove last OR in that line, Desired Output must look like

 Qualification =1 OR Qualification =2 OR Qualification =3 OR Qualification =4 

How to do this. Any help appreciated.

1

5 Answers 5

0

you can use php rtrim() function for this

foreach($Array as $key =>$value) 
{
    $TotalLine .=  " Qualification =".$value." OR ";
}
echo rtrim($TotalLine,"OR");
Sign up to request clarification or add additional context in comments.

Comments

0

Try using the end() function like this :

foreach($Array as $key =>$value){    
  $TotalLine .=  " Qualification =".$value;
  if(!end($value)){
    $TotalLine .= " OR ";
  }    
 }

Comments

0

Alternative soulution without foreach loop.

$array = [1,2,3,4];
$string = implode(" OR Qualification =", $array);
echo "Qualification =$string";

Comments

0

Simple use !empty()

PHP :

<?php
$Array =[1,2,3,4];
$TotalLine="";
foreach($Array as $key =>$value) 
{
    !empty($TotalLine) ? $TotalLine .=  " OR Qualification =".$value : $TotalLine =  "Qualification =".$value;

}
echo $TotalLine;
?>

OUTPUT :

Qualification =1 OR Qualification =2 OR Qualification =3 OR Qualification =4

Comments

0

You can use array and implode:

DEMO

$TotalLine = [];
foreach ($Array as $key => $value) {
    $TotalLine[] = " Qualification =" . $value;
}
echo implode(" OR ", $TotalLine);

Or, if you don't want this method, you can use rtrim:

DEMO

rtrim($TotalLine," OR"); 

Here, it is not needed to use the last space like " OR " since trim considers every character as a mask. Read the doc


But former one is most preferable and neat. You use arrays and then join the values using implode function. The latter one trims out the last specified string.

Be aware: When you use trim the characters in the mask will be removed. Here when your value is just numeric there will be no problem. But when you have string like ROAR your last R gets trimmed out as well. Demo for it too. Just for your knowledge! Or, this article that evolved out of this question.

5 Comments

But former one is most preferable and neat - By who? You? I definitely think the second one is better for a simple reason that if a newbie looks at the code it would be easier to figure what a function named trim is doing than implode.
I strongly prefer the first option. If I was a newbie looking at the rtrim version, I'd wonder why you were building a string only to then remove some of it. This is one of those questions that's trying to fix the wrong problem - the question shouldn't be "how to I remove the last part of a string", it should be "how do I build a string with a common separator". If it was worded like that, no one would be talking about rtrim.
And understanding that the argument for rtrim() is a series of characters, and not an exact matching string is important... two spaces in this case is meaningless, and if the last characters of the last $value include O or R it will give unexpected results
@MarkBaker That is the reason why I have specified rtrim($TotalLine," OR"); and not rtrim($TotalLine," OR "); . Also I updated with the notice as well for this case: eval.in/857916
Oh, my edit and your comment came simultaneously

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.