-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.

NewBie
  • 67
  • 3
  • 11
  • In addition to [this answer](https://stackoverflow.com/a/46115530/5447994), you can also take a look at [this article](http://vijayasankar.me.pn/blog/2017/09/08/php-implode-or-trim-concatenating-strings-in-a-loop/) – Thamilhan Sep 08 '17 at 13:29

5 Answers5

0

you can use php rtrim() function for this

foreach($Array as $key =>$value) 
{
    $TotalLine .=  " Qualification =".$value." OR ";
}
echo rtrim($TotalLine,"OR");
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
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.

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
  • *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`. – Script47 Sep 08 '17 at 11:37
  • 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](https://meta.stackexchange.com/questions/66377/what-is-the-xy-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`. – iainn Sep 08 '17 at 11:57
  • 1
    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 – Mark Baker Sep 08 '17 at 11:59
  • @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: https://eval.in/857916 – Thamilhan Sep 08 '17 at 12:00
  • Oh, my edit and your comment came simultaneously – Thamilhan Sep 08 '17 at 12:06
0

Try using the end() function like this :

foreach($Array as $key =>$value){    
  $TotalLine .=  " Qualification =".$value;
  if(!end($value)){
    $TotalLine .= " OR ";
  }    
 }
Ketan Solanki
  • 697
  • 5
  • 13
0

Alternative soulution without foreach loop.

$array = [1,2,3,4];
$string = implode(" OR Qualification =", $array);
echo "Qualification =$string";
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
JYoThI
  • 11,977
  • 1
  • 11
  • 26