Avoid the last occurrence of a character concatenated in a loop

Asked

Viewed 106 times

5

The question is as follows, whether I have a foreach or which; want another repeat structure that receives data from the table and displays the categories:

foreach ($categories as $c){
            $c.nome . ' - ';
    }

assuming that this code would display as a result:

Categoria_1 - Categoria_2 - Categoria_3 -

How to make this "-" not appear after the last loop repeat, how to hide it?

My intention is that instead of displaying as shown above the final result is:

Categoria_1 - Categoria_2 - Categoria_3

with the "-" appearing neither at the beginning nor at the end, but only among the names of the categories.

  • 1

    $c.nome would be $c->nome? The $c is an object?

3 answers

6


Use the function implode:

$categorias = array("Categoria 1","Categoria 2", "Categoria 3");
$lista = implode(" - ", $categorias);

Updating:

For objects I used the array_map returns only the property nome:

$nomes = array_map(function($objeto) { return $objeto->nome; }, $categorias);
$lista = implode(" - ", $nomes);

See example working on ideone

  • that’s it, it worked out, exactly what I wanted to do :)

  • Lucio: I can do it Join with $categorias.nome? a simple array gives as in your example (and in this case I think the best option) but if the desired functionality is to concatenate/Join an object’s properties then something is missing in your solution, right? (a mapping of the initial array to another array with the properties that must be concatenated)

  • 1

    @Sergio, I edited the answer, see if that’s it

  • @good luciorubeens! +1

  • 1

    luciorubeens, you’re a genius, man

4

For that you can shorten the string end using

$string = substr($string, 0, -3); // aqui digo para retirar os ultimos 3 caracteres
// ou 
$string = rtrim($string, ' - ');  // aqui digo para retirar a string " - " da string inicial

Notice that in your loop you are not concatenating properly, you must have

$string.= $c -> nome.' - ';

What is missing is the variable $string, the operator of concatenation .= and the way you access the property of the object that should be -> and not ..

  • Sergio o - I put as you can see in the foreach example there, this example works and displays the way I put it up there, I wanted a divisor between the name of the categories only it repeats this divisor that in the case I chose "-" at the end, then this character does not come in $category but is like a string inside the loop

3

You can do it like this

foreach ($c as $categories){
          $categoria.= $c.nome.' - ';
    }
$string = strlen($categoria);

$categories = substr($categoria,0, $string-1);

Test and say something

  • Cesar the "-" I who put as you can see in the foreach example there, this example works and displays the way I put it up there, I wanted a divisor between the name of the categories only it repeats that divisor in the case I chose "-" at the end, then this character does not come in $category but is like a string inside the loop

  • I’ve now edited test this way and tell the result

  • using the implode function I was able to do the way I wanted, it was even simpler n needed nor foreach

  • Ok you can already see yes this way also resolves but as mentioned the foreach was helping this way.

  • yes, I will check all suggested and soon give the feedback, I appreciate the attention :)

  • nothing ever orders

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.